Iterative Statement D
Iterative Statement D
Or
for (expr1; expr2; expr3; expr statement)
FOR Statement
where:
Initialization is an assignment statement that is used to set the loop’s
counter
Expr 1 is executed when the for starts.
condition is a relational boolean expression that determines when the
loop will exit
Expr 2 is the limit text expression
increment defines how the loop’s counter will change each time the
loop is repeated.
Expr 3 is the update expression. It is executed at the end of each loop
statement_sequence may either be a single or a block of statement
that make up the loop body.
The for loop continues to execute until the condition is TRUE
(1) one FALSE(0), program execution resumes in the statement
following the for loop.
Note:
• Never place a semi-colon right after the for
header. This is a logical error.
• Never change the value of the for loops
counter inside the body of the loop. This will
affect the results of the program
• The increment part of the for loop is execute
after the first iteration of the loop.
Example
Write a program the will print the number 1 to 10 using a for
statement.
#include <stdio.h> SAMPLE OUTPUT:
int x;
1
main () 2
{ 3
4
for (x=1; x<=10;x++) 5
printf (“%d/n”,x); 6
7
getch (); 8
} 9
10
WHILE Statement
The while statement or while loop is an open-
ended or event-controlled loop. The while
loop iterates while the condition is TRUE(1).
When it becomes FALSE(0), the program
control passes to the line after the loop code
WHILE Statement
• The general form of the while statement is:
while (condition)
{
statement_sequence;
}
where:
condition is a relational expression that determines
when the loop will exit
statement_sequence may either be a single or a
block of statement that make up the loop body.
Example
Write a program the will print the number 1 to 10 using a while statement.
#include <stdio.h>
SAMPLE OUTPUT:
int x;
main () 1
{ 2
x=1; 3
while (x <=10) 4
{ 5
6
printf (“%d\n”, x);
7
x++; 8
} 9
getch (); 10
}
DO-WHILE Statement
• The second type of open-ended or event-
controlled loop is the do-while statement or
do-while loop.
WHILE Statement
• The general form of the while statement is:
do
{
statement_sequence;
} while (condition);
where:
condition is a relational expression that determines when the loop will exit
statement_sequence may either be a single or a block of statement that
make up the loop body.
Do-while is a variation of the while statement which checks the
condition at the bottom/ end of the loop. This means that a do-while
loop “always executes at least once”.
In the do-while loop, when the condition evaluates to TRUE(1), the loop
body will be executed but when FALSE(0), program control proceeds to
the next instruction after the do-while loop.
Example
Write a program the will get the average of all integers from 1 to 10 using do-while loop.
#include <stdio.h>
int x, sum;
float average;
main() SAMPLE OUTPUT:
{
sum = 0; The computed average is 5.50
x=1;
do
{
sum = sum +x;
x++;
} while (x<=10)
average=sum/10.00;
printf(“The computed average is %.2f\n”, average);
getch();
}