Table of Contents
The programmer may want to repeat several instructions when writing C programs until some requirements are met. To that end, C makes looping declarations for decision-making. We have three types of loops,
- For Loop
- While Loop
- Do While Loop
For Loop
In the For loop, the initialization statement is executed only one time. After that, the condition is checked and if the result of condition is true it will execute the loop. If it is false, then for loop is terminated. However, the result of condition evaluation is true, statements inside the body of for loop gets executed, and the expression is updated. After that, the condition is checked again. This process goes on until the result of the condition becomes false. When the condition is false, the loop terminates.
SYNTAX
for( initialization statement; condition)
{
//statements inside the loop
}
How for loop works?
- The initialization statement is executed only once.
- Then, the test expression is evaluated. If the test expression is evaluated to false, the
for
loop is terminated. - However, if the test expression is evaluated to true, statements inside the body of
for
loop are executed, and the update expression is updated. - Again the test expression is evaluated.
This process goes on until the test expression is false. When the test expression is false, the loop terminates.
EXAMPLE
// Print numbers from 1 to 10
#include <stdio.h>
int main() {
int i;
for (i = 1; i < 11; ++i)
{
printf("%d ", i);
}
return 0;
}
Output
1 2 3 4 5 6 7 8 9 10
While Loop
In C, the while loop is a guided entry loop. The body of the while loops is only performed if the condition is valid. The loop structure is not executed if the condition scores to incorrect.
The while loops are usually used when several instructions have to be repeated for an indefinite time.
SYNTAX
While(condition)
{
//statements inside the loop
}
How while loop works?
- The
while
loop evaluates the test expression inside the parenthesis()
. - If the test expression is true, statements inside the body of
while
loop are executed. Then, the test expression is evaluated again. - The process goes on until the test expression is evaluated to false.
- If the test expression is false, the loop terminates (ends).
EXAMPLE
// Print numbers from 1 to 5
#include <stdio.h>
int main()
{
int i = 1;
while (i <= 5)
{
printf("%d\n", i);
++i;
}
return 0;
}
Output
1 2 3 4 5
Do While Loop
Unlike while loop, the body of the do is the difference between while and … while loop is guaranteed to be done once at a time.
SYNTAX
Do
{
//statements inside the loop
}
While(condition);
How do…while loop works?
- The body of do…while loop is executed once. Only then, the test expression is evaluated.
- If the test expression is true, the body of the loop is executed again and the test expression is evaluated.
- This process goes on until the test expression becomes false.
- If the test expression is false, the loop ends.
EXAMPLE
// Program to add numbers until the user enters zero
#include <stdio.h>
int main()
{
double number, sum = 0;
// the body of the loop is executed at least once
do
{
printf("Enter a number: ");
scanf("%lf", &number);
sum += number;
}
while(number != 0.0);
printf("Sum = %.2lf",sum);
return 0;
}
Output
Enter a number: 1.5 Enter a number: 2.4 Enter a number: -3.4 Enter a number: 4.2 Enter a number: 0 Sum = 4.70
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!