List the various types of loop available in C and explain any one with the syntax and suitable example.
Index
1. What is Loop
2. Various Type of Loop
3. Example of Loop
Ans:1. for loop2. while loop3. do while loop
(1)
for loop:- for loop is used to execute statement multiple times
.
Syntax of for loop:-
for(initialization ; condition ; increment/decrement){
//body of
loop
}
Example:-
#include <stdio.h>
int main()
{
for( int i =
5; i<10;i++)
{
printf("%d\n",i);
}
return 0;
}
(2) while
loop:-The while loop is a control flow statement which is
used to execute the statement repeatedly till the condition statement is true.
Syntax for while loop:-
while (boolean condition)
{
loop
statements...
}
Example:-
#include <stdio.h>
int main()
{
int i = 5;
while
(i< 10) {
printf("%d\n",i);
i++;
}
return 0;
}
(3)
do-while:-
Do while loop is similar to while loop with a difference that do-while loop
execute the state then after that is check the condition.
Syntax for do-while loop:-
do
{
statements..
}
while (condition);
Example:-
#include <stdio.h>
int main()
{
int i = 5;
do {
printf("%d\n",i);
i++;
} while
(i< 10)
return 0;
}
No comments:
Post a Comment