We have seen in the previous that it is possible to execute a segment of program repeated by introduced a counter and later testing if statements .while this method is quite satisfactory for all practical purpose, we need to initialize and increment a counter and test its value at an appropriate in the program for the completion of the loop. For example, suppose we want to calculate the sum of squares of all the integers between 1 to 10. we can write a program using the if statement as follows:-
While
The simplest of all the looping structures in C the While statement. It is an entry controlled-loop. The condition is evaluated if the condition is true only then the statement(body) of the loop is executed. after execution of the body, the condition is once again evaluated and if again it true, the body is executed once again. The repetition execution of the body continues until the condition finally becomes false and control is transferred out of the loop.
Its syntax is:
1 2 3 4 |
while (condition) { statement ; } |
C Code Example
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <stdio.h> void main() { int i=0; printf(" Number is =") while(i<10) { printf("%d\n",i); i=i+1; } } |
do-while
The While loop construct that we have discussed in the previous section, makes a test of condition before the loop is executed. Therefore , the body of the loop may not be executed at all if the conditions are not satisfied at the very first attempt. On some occasions, it might be necessary to execute the body of the loop before the test is performed. such situations can be handled with help of a do statement.
Its syntax is:
1 2 3 4 5 6 |
do { statement ; } while (condition); |
C Code Example
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <stdio.h> void main() { int i=0; do{ printf("\n%d",i); i++; }while(i<10) } |
for loop
The for-loop is another entry controlled-loop that provides a more concise loop control structure.
Its syntax is:
1 2 3 4 |
for(initialization ; condition ; Increment/decrement) { statement(body); } |
The execution of for statement is as follow:-
Initialization of the control variable is done first, using assignment statements such as a=0.
The value of the control variable is tested using the condition. If the condition is true, then the body of the loop is executed otherwise loop is terminated.
When the body of the loop is executed, then the control transferred back to for statement. Now the control variable is incremented or decremented as accordingly loop arguments and then the new value of the loop is again tested to see whether the loop is satisfied the loop conditions. If true, then the body is again executed otherwise loop terminated.
Nesting of For loop
Nesting of loops, that is, one for statement within for statement, is allowed in C.For example:-
1 2 3 4 5 6 7 8 9 10 11 |
for(int i=0; i < 10; i++) { for(int j=0; j < 5; j++) { Inner-loop-body; } outer-loop-body; } * The nesting may continue up to any desired level. |
C Code Example
1 2 3 4 5 6 7 8 9 10 11 |
#include <stdio.h> void main() { int i; for(i=0; i<10; i++) { printf("\n %d",i); } } |