Introduction
Instructions in a program are executed sequentially. But, most of the time it is required to repeat certain steps to meet a need of user or programmer. This involves repeating some portion of the program either specified number of times or until a particular condition is being satisfied. This iterative operation is done through a loop control statements. The loop statements supported by C language are:
1. The while loop
2. The for loop
3. The do-while loop
The while Loop
The while loop repeats a statement or a set of statements until a certain condition is no longer true. The general form of while loop
Initialize loop counter;
while (test condition)
{
statement-1;
statement-2;
.
.
Increment/decrement loop counter;
}
One of the variables used frequently when writing programs involving loops is the counter variable. This loop counter keeps track of the number of times a loop has been performed. In the while loop, after the keyword while a test condition is specified. As long as this condition remains true, all the statements within the body of while loop keep getting executed repeatedly. As soon as the conditional expression becomes false, the loop terminates and the control passes to the statement that follows the while loop construction. If there is only one statement in the body of while loop then the use of braces can be omitted. As a rule, the while must test a condition that will eventually become false, otherwise the loop would be executed forever, indefinitely.
The for Loop
The "for loop" is probably the most popular loop control statement. It is an abbreviated version of the "while loop". In the while loop the counter variable is first initialized, then the test expression is evaluated and the loop is executed. The body of the loop then ends with a statement, which changes the value of loop counter used in the conditional expression. All these steps can be combined into a single statement with the help of for loop. Thus, for loop allows us to specify three things about a loop in a single line.
1. Setting a loop counter to an initial value.
2. Testing the loop counter to determine whether its value has reached to the number of repetitions desired.
3. Incrementing or decrementing the value of loop counter each time the program segment within the loop body has been executed.
The general syntax of for loop:
for (initialize loop counter; test condition; increment/decrement clause)
{
statement-1;
statement-2;
.
.
statement-n;
}
When for loop is executed first time, the value of loop counter is set to its initial value. Now condition is tested and if it is true, the statements in the body of the loop get executed, otherwise the loop gets terminated.
As the control reaches to the last statement of the loop body, the control comes back to the increment/decrement clause where the value of loop counter is changed. Now the condition is tested again to check whether it is true or false. This process gets repeated until the condition becomes false and loop gets terminated.
If there is only one statement in the body of for loop then the use of braces can be omitted. As a rule, for statement must test a condition that will eventually become false, otherwise the loop would be executed forever, indefinitely.
In the initialization clause of for loop, one can initialize multiple variables simultaneously. Similarly, one can also specify more than one variable in the increment or decrement clause of for loop.
for (i = 1, j=10; j > = 0; i ++, j --)
{
.........;
}
The do while Loop
The above explained two loop control statements for and while, test a given condition before the loop is executed. Thus, the body of loop may never be executed if the condition is not satisfied.
When developing programs, one might wish to test the conditional expression at the end of the loop rather than at the start of the loop. The do-while loop fulfill this requirement.
The general syntax of do-while loop;
do
{
statement-1;
statement-2;
.
.
statement-n;
} while (test condition);
Difference between while and do while Loop
The difference between while and do-while loop is that, the while loop tests the condition before the loop body is executed. On the other hand, the do-while tests the condition after having executed the statements within the body. Thus, do-while executes the loop body at least once, even if the condition fails for the first time itself. While loop is known as entry controlled loop but do while loop is known as exit controlled loop.
The nested for Loops
We can have for loop within another for loop as per the program requirement. This construct is known as nested for loops. A for loop already contained in for loop can contain another for loop as well and so on. There is no limit on how deep the loops can be nested.
for (initialize clause; conditional expression; increment/decrement clause)
{
statements;
for (initialize clause; conditional expression; increment/decrement clause)
{
statements;
}
statements;
}
Similar to for loops, the while loops as well as do-while loops can be nested, not only this a while loop can appear in for loop or in do-while loop and vice-versa.
void main ( )
{
int r, c, sum;
for (r = 1; r <=3; r++)
{
for (c = 1; c <= 3; c++)
{
sum = r + c;
printf ("r = %d c = %d sum = %d \n", r, c, sum);
}
}
}