0% found this document useful (0 votes)
54 views13 pages

Iterative Statement D

Iterative statements allow a set of instructions to be repeatedly executed until a condition is met. The three main types are for, while, and do-while loops. For loops iterate a predetermined number of times based on an initialization, condition, and increment. While loops iterate as long as a condition is true. Do-while loops always execute the body at least once and then iterate while the condition is true.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views13 pages

Iterative Statement D

Iterative statements allow a set of instructions to be repeatedly executed until a condition is met. The three main types are for, while, and do-while loops. For loops iterate a predetermined number of times based on an initialization, condition, and increment. While loops iterate as long as a condition is true. Do-while loops always execute the body at least once and then iterate while the condition is true.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

ITERATIVE STATEMENTS

Iterative statements (loops)


Iterative statements allows a set of instructions
to be executed or performed several times
until certain conditions are met. It can be
predefined as in the for loop or open-ended as
in while and do-while.
1. FOR statement
2. WHILE statement
3. DO-WHILE statement
FOR Statement
• The for statement or for loop is considered as
a predefined loop because the number of
times it iterates to perform its body is
predetermined in the loop’s definition .
• The for loop contains a counter whose values
determine the number times the loop iterates.
The iteration stops upon reaching the number
of times specifies in the loop.
FOR Statement
• The general form of the for statement is:
for (initialization; condition; increment)
{
statement_sequence;
}

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();
}

You might also like