Date: agosto 25, 2025
Author: Guillermo Garcia
Categories: Embedded C programming Tags: Embedded C programming
In this article we will see the repetitive statements to create loops to create programs in C language for embedded systems.
Table of Contents
In every programming language there are circumstances were you want to do the same thing many times. For instance you want to print the same words ten times. You could type ten printf functions, but it is easier to use a loop. To this end, C provides 3 repetition statements:
The for statement is used for counter-controlled repetition. It allows to iterate a block of statements a predefined number of times. In its most general form, the for loop can be described as:
for(<initialization>; <test expression>; <step expression>)
{
<statement>;
<statement>;
}
<next statement>;
A counter variable is used to count the number of repetitions. At the start of the for loop, the counter variable needs to be initialized once as expressed in the expression. As long as the is met (result ≠ 0), the block statements are executed and the counter variable is increased / decreased. The describes how the counter variable needs to be changed after every loop execution. When the counter variable no longer matches the (the test yields 0), the loop is terminated and execution continues with the after the for loop.
#include <stdio.h>
#define REP_TIMES 10
int main(void)
{
int number, counter, sum=0;
for (counter = 0; counter < REP_TIMES; counter++)
{
printf("Enter an integer number: ");
scanf("%d%*c", &number);
sum += number;
}
printf("\nThe sum of these numbers equals: %d.\n", sum);
return 0;
}
Output:
Enter an integer number: 1 Enter an integer number: 2 Enter an integer number: 3 Enter an integer number: 4 Enter an integer number: 5 Enter an integer number: 6 Enter an integer number: 7 Enter an integer number: 8 Enter an integer number: 9 Enter an integer number: 10 The sum of these numbers equals: 55
The while statement is used for condition-controlled repetition. It allows to iterate a block of statements as long as a condition remains true. In this case it is usually not know in advance how many times the loop will be executed. Of course, the loop must be written such that the condition will become false at some point. In its most general form, the while loop can be described as:
<initialization>;
while (<condition>)
{
<statement>;
<statement>;
…
<change condition variables>;
}
<next statement>;
Some condition variables are used to determine whether the loop needs to be executed again or not. These condition variables need to be initialized before the start of the while loop in the statement. As long as the is true (result ≠ 0), the block statements are executed. To avoid ending up with an infinite loop, at least one of these loop statements needs to take care of changing the condition variables such that in the end the condition will become false (0). When this happens, the loop is terminated and execution continues with the after the while loop.
#include <stdio.h>
int main(void)
{
int number, sum;
sum = 0;
printf("Enter an integer number (end with 999): ");
scanf("%d%*c", &number);
while (number != 999)
{
sum += number;
printf("Enter an integer number (end with 999): ");
scanf("%d%*c", &number);
}
printf("\nThe sum of these numbers equals: %d.\n", sum);
return 0;
}
Output:
Enter an integer number (end with 999): 2 Enter an integer number (end with 999): 6 Enter an integer number (end with 999): 3 Enter an integer number (end with 999): 4 Enter an integer number (end with 999): 5 Enter an integer number (end with 999): 999 The sum of these numbers equals: 20.
The do … while repetition statement is similar to the while repetition statement. In its most general form, the do … while loop can be described as:
<initialization>;
do
{
<statement>;
<statement>;
…
<change condition variables>;
}while(<condition>);
<next statement>;
In the do … wile statement, the condition is tested after the execution of the loop statements, whereas in the while statement, the condition is tested first. As a result, the loop statements in a do … while loop are
always executed at least once. In a while loop on the other hand, it is possible that the loop statements are never executed.
#include <stdio.h>
int main(void)
{
int number, sum;
sum = 0;
number = 0;
do
{
sum += number;
printf("Enter an integer number (end with 999): ");
scanf("%d%*c", &number);
} while (number != 999);
printf("\nThe sum of these numbers equals: %d.\n", sum);
return 0;
}
Output:
Enter an integer number (end with 999): 6 Enter an integer number (end with 999): 4 Enter an integer number (end with 999): 3 Enter an integer number (end with 999): 5 Enter an integer number (end with 999): 2 Enter an integer number (end with 999): 999 The sum of these numbers equals: 20.
break and continue statements are used to change the program flow in loops.
we discussed the use of break in a switch statement.
There the break was used to immediately stop the execution of the switch and continue with the next statement after the switch.
A break statement can also be used in a for, while or do … while loop. In these cases, the break statement will immediately terminate the execution of the nearest enclosing loop and will pass control to the
statement following that loop.
Read integer numbers until the number 999 is entered and print their sum. If the sum exceeds 50, the loop must be terminated as well.
A possible solution is to use a break statement inside the loop:
#include <stdio.h>
int main(void)
{
int number, sum;
sum = 0;
printf("Enter an integer number (end with 999): ");
scanf("%d%*c", &number);
while (number != 999)
{
sum += number;
if (sum > 50) break;
printf("Enter an integer number (end with 999): ");
scanf("%d%*c", &number);
}
printf("\nThe sum of these numbers equals: %d.\n", sum);
return 0;
}
When a continue statement is used in a for, while or do … while loop, the remainder of the loop statements is skipped and the next loop iteration is performed.
In the case of a while or do … while control structure, the loop condition is tested immediately after the execution of the continue statement. In a for repetition statement on the other hand, the step expression is executed first before evaluating the loop condition.
Read integer numbers until the number 999 is entered and print the sum of all positive numbers read. A possible solution using a continue statement.
#include <stdio.h>
int main(void)
{
int number, sum;
sum = 0;
printf("Enter an integer number (end with 999): ");
scanf("%d%*c", &number);
while (number != 999)
{
if (number < 0)
{
printf("Enter an integer number (end with 999): ");
scanf("%d%*c", &number);
continue;
}
sum += number;
printf("Enter an integer number (end with 999): ");
scanf("%d%*c", &number);
}
printf("\nThe sum of these numbers equals: %d.\n", sum);
return 0;
}

Deja una respuesta