home
C TUTORIALS

Loop

while
for
do-while

The while Loop

Often used in the case when you want to do something a fixed number of times.
The general form of while is as shown below:

initialise loop counter; while(test loop counter using a condition) { do this; and this; increment loop counter; }
Note the following points about while...
(a)The statement within the while loop would keep on getting executed till the condition being tested remains true.When the condition becomes false,the control passes to the first statement that follows the body of the while loop.
(b)The condition being tested may use relational or logical operators.
(c)The statement within the loop may be a single line or block of the statements.In first case braces are optional.
(d)Almost always, the while must test a condition that will eventually become false, otherwise the loop would be executed forever i.e. infinite loop.
(e)We can increment as well as decrement loop counter.Increment and decrement could be any value not necessarily 1.

The for Loop

It is probably the most popular looping instruction.It allows us to specify three things about a loop in a single line:-
(a)Setting a loop counter to an initial value.
(b)Testing the loop counter to determine whether its value has reached the number of repetitions desired.
(c)Increasing the value of loop counter each time the program segment within the loop has been executed.
The general form of for statement is as under:
for(initialise counter;test counter;increment counter) { do this; and this; and this; }
NOTE:1. The initialization, testing and incrementation part of a for loop can be replaced by any valid expression.
2.The way the if statements can be nested, similarly while and for can also be nested.
For example:
for(i=10;i>0;i--) printf("%d",i); for(i<4;j=5;j=0) printf("%d",i); for(i=1;i<=10; printf("%d",i++)) for(scanf("%d",&i);i<=10;i++) printf("%d",i);

Multiple Initialisations in the for Loop

The initialisation expressions in the for loop can contain more than one statement separated by a comma.
For example:
for(i=1,j=2;j<=10;j++)
Multiple statements can also be used in the incrementation expression of for loop i.e. you can increment (or decrement) two or more variables at the same time.However, only one statement is allowed in the test expression. This expression may contain several conditions linked together using logical operators.

The do-while Loop

The general form of this do-while loop is:

do { this; and this; and this; and this; }while(this condition is true);
There is major difference between the working of while and do-while loops.This difference is the place where the condition is tested.The while tests the condition before executing any of the statements whereas do-while tests the condition after having executed the statements within the loop.
This means that do-while would execute its statements at least once, even if the condition.fails for the first time.The while, on the other hand will not execute its statements if the condition fails for the first time.