Looping statements are used to execute a specific group of statements repeatedly according to a given condition.
while statements
Do..while statements
For statements
while loop statements
While statements check the condition first. If the condition is true, the group of statements will be executed. Again, the same condition will be checked. If the condition is true again, the group of statements will be executed. For example, until the condition is true, that particular group of statements will be executed. is being executed.
syntax:-
while(condition)
{
statement 1;
statement 2;
}
#include < stdio.h >
#include < conio.h >
void main()
{
int i;
i=1;
while(i<5);
{
printf("welcome");
i++;
}
getch();
}
output:
welcome
welcome
welcome
welcome
for loop statements:
The for statement is used to repeatedly execute a statement or a group of statements.
syntax:-
for(initialization;textcondition;increment/decrement)
{
body of the loop;
}
next statement;
Example:
#include < stdio.h >
void main()
{
for(i=0;i<=5;i++)
{
printf("c tutorial");
}
getch();
}
output:
c tutorial
c tutorial
c tutorial
c tutorial
c tutorial
do…while statements
In do...while statements, the body of the loop will be executed first. After that, the condition will be checked. If the condition is true, the body of the loop will be executed again. In this way, the body of the loop will be executed until the condition is false. - will be executed. If condition becomes false then control will go to next statement.
syntax:-
do
{
body of the loop;
}
while(condition);
next statement;
Programme:
#include < stdio.h >
void main()
{
int i;
i=1;
do
{
printf("welcome");
i++;
}while(i<5);
getch();
}
output:
welcome
welcome
welcome
welcome