For Repetition Statement
The for repetition statement handles all the details of counter-controlled repetition
Example
/*Counter-controlled repetition with the for statement */
#include <stdio.h>
int main ( void )
{
int counter ;
/*initialization, repetition condition, and increment are all included in the for statement header. */
for( counter = 1; counter<= 10; counter++)
{
printf(“%d\n”, counter );
}
return 0;
}
The program operates as follows. When the for statement begins executing, the control variable counter is initialized to 1. Then, the loop-continuation condition counter <= 10 is checked. Because the initial value of the counter is 1, the condition is satisfied, so the printf statement prints the value of the counter, namely 1. The control variable counter is then incremented by the expression counter++, and the loop begins again with the loop-continuation test. Since the control variable is now equal to 2, the final value is not exceeded, so the program performs the printf statement again. This process continues until the control variable counter is incremented to its final value of 11 this causes the loop-continuation test to fail, and repetition terminates. The program continues by performing the first statement after the for statement (in this case, the return statement at the end of the program).
Notice that uses the loop-continuation condition counter <= 10. If you
incorrectly wrote counter < 10, then the loop would be executed only 9 times. This is a common logic error called an off-by-one error.
Using an incorrect relational operator or using an incorrect initial or final value of a loop counter in the condition of a while or for statement can cause off-by-one errors. Using the final value in the condition of a while or for statement and using the <= relational operator will help avoid off-by-one errors. For a loop used to print the values 1 to 10, for example, the loop-continuation condition should be counter <= 10 rather than counter < 11 or counter < 10. The general format of the for statement is
for ( expression1; expression2; expression3 )
the statement where expression1 initializes the loop-control variable, expression2 is the loop-continuation condition, and expression3 increments the control variable. In most cases, the for statement can be represented with an equivalent while statement as follows:
expression1;
while ( expression2 ) {
statement
expression3;
}
Often, expression1 and expression3 are comma-separated lists of expressions. The commas as used here are actually comma operators that guarantee that lists of expressions evaluate from left to right. The value and type of a comma-separated list of expressions are the value and type of the right-most expression in the list. The comma operator is most often used in the for the statement. Its primary use is to enable you to use multiple initializations and/or multiple increment expressions. For example, there may be two control variables in a single for the statement that must be initialized and incremented. Place only expressions involving the control variables in the initialization and increment sections of a for the statement. Manipulations of other variables should appear either before the loop (if they execute only once, like initialization statements) or in the loop body (if they execute once per repetition, like incrementing or decrementing statements).
The three expressions in the for statement are optional. If expression2 is omitted, C assumes that the condition is true, thus creating an infinite loop. One may omit expression1 if the control variable is initialized elsewhere in the program. expression3 may be omitted if the increment is calculated by statements in the body of the for a statement or if no increment is needed. The increment expression in the for statement acts as a stand-alone C statement at the end of the body of the for. Therefore, the expressions
counter = counter + 1
counter += 1
++counter
counter++
are all equivalent in the increment part of the for the statement. Many C programmers prefer the form counter++ because the incrementing occurs after the loop body is executed, and the post incrementing form seems more natural. Because the variable being pre incremented or post incremented here does not appear in a larger expression, both forms of incrementing have the same effect. The two semicolons in the for statement are required. Using commas instead of semicolons in a for the header is a syntax error. Placing a semicolon immediately to the right of a for header makes the body of that for statement an empty statement. This is normally a logic error.
for Statement: Notes and Observations
- The initialization, loop-continuation condition and increment can contain arithmetic expressions. For example, if x = 2 and y = 10, the statement
for ( j = x; j <= 4 * x * y; j += y / x ) is equivalent to the statement for ( j = 2; j <= 80; j += 5 )
- The “increment” may be negative (in which case it’s really a decrement and the loop actually counts downward).
- If the loop-continuation condition is initially false, the loop body does not execute. Instead, execution proceeds with the statement following the for the statement.
- The control variable is frequently printed or used in calculations in the body of a loop, but it need not be. It’s common to use the control variable for controlling repetition while never mentioning it in the body of the loop.
- Although the value of the control variable can be changed in the body of a for loop, this can lead to subtle errors. It’s best not to change it.
Example
The following examples show methods of varying the control variable in a for the statement.
/*summation with for*/
#include <stdio.h>
.
int main( void )
{
int sum = 0;
int number;
.
for ( number = 2; number<=100; number += 2 ){
sum+= number;
}
/*end for*/
.
printf(“sum is %d\n”, sum );
return 0;
}
Output:
Sum is 2550
The body of the for statement could actually be merged into the rightmost portion of the for a header by using the comma operator as follows:
for ( number = 2; number <= 100; sum += number, number += 2 )
; /* empty statement */
The initialization sum = 0 could also be merged into the initialization section of the for. Although statements preceding a for and statements in the body of a for can often be merged into the for the header, avoid doing so because it makes the program more difficult to read.
Limit the size of control-statement headers to a single line if possible