C Looping Statement
C Looping Statement
C Looping Statement
When you need to execute a block of code several number of times then you
need to use looping concept in C language.
1. while loop
2. for loop
3. do while loop
WHILE LOOP
while loop can be addressed as an entry control loop. It is completed in 3 steps.
Syntax :
variable initialization;
while(condition)
statements;
#include<stdio.h>
main( )
int x;
x = 1;
printf("%d\t", x);
x++;
getche();
}
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..
SYNTAX:
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.
The for loop is executed as follows:
statement ;
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.
SYNTAX:
do
{
<statement/s>;
.....
}
while(condition)
Example: Program to print first 10 multiples of 5.
#include<stdio.h>
main()
int a, i;
a = 5;
i = 1;
do
printf("%d\t", a*i);
i++;
getche();
}
Output:
5 10 15 20 25 30 35 40 45 50