Loop In C
Loop method is used when we have to repeatedly execute some block of statements as long as the condition is true.
Let us take an example to understand the requirement of loop method –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
/*program to print hello 5 times without loop*/ #include<stdio.h> int main() { printf("\nhello"); printf("\nhello"); printf("\nhello"); printf("\nhello"); printf("\nhello"); return 0; } /*program to print hello 4 times with loop*/ int main() { int count; for(count=0;count<5;count++) { printf("\nhello"); } return 0; } |
So we can see that we have to write 5 times printf statement to print 5 times, hello and it can be more complex if we want to execute 1000 times or more while the use of for loop or any loop method makes it simple in just 2 lines whether it is 1000 or 1 million times.
To get rid of this problem C language introduces a powerful concept of loop method
1 2 3 4 5 6 7 8 9 10 |
/*program to print hello 4 times with loop*/ int main() { int count; for(count=0;count<5;count++) { printf("\nhello"); } return 0; } |
There are following methods in loop category –
syntax
1 2 3 4 5 6 7 8 9 10 |
Syntax 1: for(expression1;expression2;expression3) { statement1; statement2; . . . statementn; } |
expression1 which is the initialization of loop only execute at the first iteration of the loop.
expression2 is the conditional statement of the loop and it will check at the start of every iteration of the loop.if the condition is false then the loop will terminate otherwise execution of loop will continue.
expression3 is the increment/decrement of the loop count.it is the last statement of every iteration of the loop.
expression 1, expression 2, expression3 positions are not fixed.For loop has other ways to represent –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
Syntax 2: Initialization outside the loop expression1; //initialization for(;expression2;expression3) { statement1; . statementn; } Syntax 3: Only condition check expression in for loop syntax expression1; //initialization for(;expression2;) { statement1; . statementn; expression3; //increment or decrement operation } Syntax 4: Infinte loop creation for(;;) { statement1; . statementn; } |
Example
check the all above syntax with this example and see the behavior.
1 2 3 4 5 6 7 8 9 10 11 12 |
/*program to print hello 5 times without loop*/ int main() { int count; for(count=0;count<5;count++) { printf("\nhello"); } return 0; } |
syntax
1 2 3 4 5 6 7 8 |
while(condition) { statement1; statement2; . . statementn; } |
For Loop Syntax equivalent to while
1 2 3 4 5 6 7 8 9 10 |
expression1; /*initialisation*/ while(expression2) /*conditional expression*/ { statement1; statement1; . . statementn; expression3; /*increment or decrement operation*/ } |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 |
/*program to print hello 5 times with while loop*/ int main() { int count=0; while(count<5) /* equivalent for loop statement is for(;count<5;)*/ { printf("\nhello"); count++; } return 0; } |
Once the statement is to be executed whether the condition is true or false in the do-while loop method.
Therefore, the use of the do-while method is done in such a place, where we have to execute the statement once before the condition check.
Syntax
1 2 3 4 5 6 7 8 9 |
do { statement1; statement2; . . statementn; expression3; }while(condition); |