Loops
Loops
Objective
Introduction
Types of looping
while
do-while
for
Assessment metric
Conclusion
References
What is Loop?
Statements
Conditio
n
Evaluate
d
true
false
true Conditio
n
Statements Evaluate
d
false
TYPES OF
LOOP
while loop
do-while loop
for loop
while Loop
It has a loop condition only that is tested before each
iteration to decide whether to continue or terminate the
loop.
The body of a while loop will execute zero or
more times
Syntax:
while (<condition>){
<statement/block>;
}
Example : Flow diagram
int i=0;
while(i<3){ Conditio
n
Evaluate
printf(“Hello\n”); d
false
true
i++;
Statements
}
Output
: Hello
Hello
Hello
do…while Loop
Do while has a loop condition only that is tested after
each iteration to decide whether to continue with next
iteration or terminate the loop.
Syntax:
do{
<statement/block>;
}while(condition);
Example:
int i=0; Flow diagram
do{
Printf (“Hello\n”);
i++;
Statements
} while (i<3);
true
Output:
Conditio
n
Evaluate
d
Hello false
Hello
Hello
for
Loop
for loop has three parts:
Flow diagram
initialization
Conditio
n
Evaluate
d
true false
Statements
increament
Example:
for(i=0; i<3; i++)
{
printf(“Hello\n”);
}
Output:
Hello
Hello
ASSESSMENT METRIC
List out the difference between while and for loop. And also
explain the do-while loop.
CONCLUSION
Importance of loops in any programming language is
immense, they allow us to reduce the number of lines in a
code, making our code more readable and efficient.