Contents
For loops generally look something like this:
for(initialization; test; increment)
{
/* code */
}
The initialization statement is executed exactly once - before the first evaluation of the test condition. Typically, it is used to assign an initial value to some variable, although this is not strictly necessary. The initialization statement can also be used to declare and initialize variables used in the loop.
The test expression is evaluated each time before the code in the for loop executes. If this expression evaluates as 0 (false) when it is checked (i.e. if the expression is not true), the loop is not (re)entered and execution continues normally at the code immediately following the FOR-loop. If the expression is non-zero (true), the code within the braces of the loop is executed.
After each iteration of the loop, the increment statement is executed. This often is used to increment the loop index for the loop, the variable initialized in the initialization expression and tested in the test expression. Following this statement execution, control returns to the top of the loop, where the test action occurs. If a continue statement is executed within the for loop, the increment statement would be the next one executed.
Each of these parts of the for statement is optional and may be omitted. Because of the free-form nature of the for statement, some fairly fancy things can be done with it. Often a for loop is used to loop through items in an array, processing each item at a time.
int myArray[12];
int ix;
for (ix = 0; ix<12; ix++)
myArray[ix] = 5 * ix + 3;
The above for loop initializes each of the 12 elements of myArray. The loop index can start from any value. In the following case it starts from 1.
for(ix = 1; ix <= 10; ix++)
{
printf("%d ", ix);
}
which will print
1 2 3 4 5 6 7 8 9 10
You will most often use loop indexes that start from 0, since arrays are indexed at zero, but you will sometimes use other values to initalize a loop index as well.
The increment action can do other things, such as decrement. So this kind of loop is common:
for (i = 5; i > 0; i--)
{
printf("%d ",i);
}
which yields
5 4 3 2 1
Here's an example where the test condition is simply a variable. If the variable has a value of 0 or NULL, the loop exits, otherwise the statements in the body of the loop are executed.
for (t = list_head; t; t = NextItem(t) ) {
/*body of loop */
}
A WHILE loop can be used to do the same thing as a FOR loop, however a FOR loop is a more condensed way to perform a set number of repetitions since all of the necessary information is in a one line statement.
A FOR loop can also be given no conditions, for example:
for(;;) {
/* block of statements */
}
This is called a forever loop since it will loop forever unless there is a break statement within the statements of the for loop. The empty test condition effectively evaluates as true.
It is also common to use the comma operator in for loops to execute multiple statements.
int i, j, n = 10;
for(i = 0, j = 0; i <= n; i++,j+=2)
printf("i = %d , j = %d \n",i,j);
All text is available under the terms of the GNU Free Documentation License
Source : Wikibooks