The for statement

Repetition in Programming
1
Concept of a Loop
The following is a concept of a loop:
Series of actions
This a a continuous loop. To make sure that it ends,
we put conditions to control the loop.
2
Pre-Test and Post-Test Loops
In a pre-test loop, the loop control
expression is tested first. If it is true, the
loop actions are executed. If it is false,
the loop is terminated.
In the post test loop, in each iteration, the
loop actions are executed. Then, the loop
control expression is tested. If it is true, a
new iteration is started, otherwise the
loop is terminated.
3
Initialization and Updating
In addition to the loop control expression, two
other processes are associated with almost all loops,
initialization and updating.
Before a loop can start, some preparation is
usually required. Initialization sets the stage for the loop
action.
In order for the loop to not continue indefinitely,
the condition inside the body of the loop has to also
change. This is called updating.
4
Loops in C
C has three loop statements : the while,
the for and the do…while statements. The first
two are pre-test loops and the do..while is a
post-test loop.
All of these loop constructs continue when
the limit control test is true and terminate when
it is false. This consistency in design makes it
easy to write the limit test in C.
5
The while loop
Since the while statement is a pretest loop, it tests
the expression before every iteration of the loop. The
basic syntax of the statement is as follows:
expression
statements
while ( expression )
{
statements
}
6
The for statement
The for statement, like the while statement, is a
pretest loop that users three expressions.
expr1
expr2
false
for ( expr1; expr2; expr3 )
statement
true
statement
expr3
7
for statement cont.
for ( expr1; expr2; expr3 )
statement
expr1 -initializes the loop
expr2 -used for testing the condition
expr3 -used to update the loop
statement -the body of the loop
8
for statement cont.
k = 6;
for( j=2; k%j == 0; ++j )
{
printf( “%d is a divisor of %d\n”,j,k );
sum += j;
}
9
for statement cont.
Any or all of the expressions in a for statement can be
missing, but the two semicolons must remain.
eg1:
sum = 0;
for ( i=0; i <= 10; ++i )
sum += i;
eg2:
i = 1;
sum = 0;
for ( ; i <= 10; ++i )
sum += i;
10
for statement cont.
eg3:
i = 1;
sum = 0;
for ( ; i <= 10; )
sum += i++;
eg4:
i = 1;
sum = 0;
for ( ; ; )
{
sum += i++;
printf( “ %d\n”, sum );
}
11
for statement cont.
If the body of the for loop contains no continue statement, the for
statement can be logically equivalent to a while statement.
for( expr1; expr2; expr3 )
action
is logically equivalent to
expr1;
while( expr2 )
{
action
expr3;
}
12
The do..while loop
The do..while loop is a post-test loop. The format of
the do..while loop is as shown:
statement
true
do
{
statements;
} while ( expression );
expression
false
13
The comma expression
The comma expression is a complex expression made
up of two expressions separated by commas. Although
it can be legally used in many places, it is most often
used in for statements. The expressions are evaluated
left to right. The comma expression has the lowest
priority of all expressions.
for ( sum = 0, x=1; x <= 20; x++ )
{
scanf_s( “%d”, &a );
sum += a;
}
14
Jump statements
There are other C statements that can be used to
change the sequence of control. One such statement is
the return statement, that has been already discussed.
There is another statement, which is the goto
statement, which is not really great for structured
programs.
The other two jump statements are the break and the
continue statement, which are related to loops.
15
The break statement
There are other C statements that can be used to
change the sequence of control. One such statement is
the return statement that has been already discussed.
There is another statement, which is the goto
statement, which is not really great for structured
programs.
The other two jump statements are the break and the
continue statement, which are related to loops.
16
The break statement
The break statement causes an exit from the innermost
enclosing loop.
while( condition )
{
for( i=10; i>1; i--)
{
if( i == 3 )
break;
printf( ”%i\n”, i);
}
}
The break statement can be used in any loop statement, but it is
best to use only in switch statements, for structured programming.
17
The continue statement
The continue statement skips the rest of the loop body and causes
the loop test to be immediately evaluated.
for( i=1; i<=3; i++ )
{
printf( %d\n”, i );
if( i == 2 )
continue;
printf( "Bottom of loop\n");
}
printf( "Out of loop\n" );
18