Pps Class 10
Pps Class 10
Course on
Programming for Problem
Solving
: Loops
LOOPS IN C
• In any programming language including C, loops are used to
execute a set of statements repeatedly until a particular condition
is satisfied.
if the Test Condition is true, then the loop is
executed, and if it is false then the execution
breaks out of the loop. After the loop is
successfully executed the execution again starts
from the Loop entry and again checks for the Test
condition, and this keeps on repeating.
1.while loop
2.for loop
3.do while loop
WHILE LOOP
• while loop can be addressed as variable initialization;
while(condition)
an entry control loop. It is
{
completed in 3 steps. statements;
variable increment or decrement;
}
• Variable initialization.(e.g int x =
#include<stdio.h>
0;) void main( )
• condition(e.g while(x <= 10)) {
int x;
• Variable increment or decrement x = 1;
( x++ or x-- or x = x + 2 ) while(x <= 10)
{
printf("%d\t", x);
x++;
}
}
FOR LOOP
• for loop is used to execute a set of statements repeatedly until a
particular condition is satisfied. We can say it is an open ended
loop.. General format is,
for(initialization; condition; increment/decrement)
{
statement-block;
}
In for loop we have exactly two semicolons, one after initialization and second after the
condition. In this loop we can have more than one initialization or increment/decrement,
separated using comma operator. But it can have only one condition.
void main( )
{
int x;
for(x = 1; x <= 10; x++)
{
printf("%d\t", x);
}
}
DO WHILE LOOP
• In some situations it is necessary to execute body of the loop before testing
the condition. Such situations can be handled with the help of do-while loop.
do statement evaluates the body of the loop first and at the end, the condition
is checked using while statement. It means that the body of the loop will be
executed at least once, even though the starting condition inside while is
initialized to be false. General syntax is,
#include<stdio.h>
do
{ void main()
{
.....
int a, i;
..... a = 5;
} i = 1;
do
while(condition);
{
printf("%d\t", a*i);
i++;
}
while(i <= 10);
}
Thank You