LOOP- C language provide three types of loop
- For loop
- while loop
- do while loop
For loop
- This is most commonly used loop in C Language.
- The syntax and flow of this loop is simple and easy to learn.
- for loop is an entry controlled loop.
- for loop is preferred statement is to be executed for fixed or know number of times.
- for loop consists of three components.
- Syntax for loop-for(Initialization;condition;Increment/Decrement)
- Ex- Using for loop print 1 to 10 number.
#include<conio.h>
void main()
{
int x;
clrscr();
for(x=1:x<=10;x++)
{
printf("%d\n",x);
}
getch();
}
while loop-
- while loop is also Entry controlled loop.
- while loop conditions are checked if found true then and then only code is executed.
- Initialization, Inreamantaion and condition steps are no different line.
- While(1)- While(1) is used for Infinite loop.
- Single line code opening and closing braces are not needed.
- While loop is used to repeat a section of code an unknown number of times until a specific condition is met.
- While loop syntax;
Initialization;while(condition)
{
.........................
.........................
Increment;
}
![]() |
While loop flow chart |
- Ex- Using while loop print 1 to 10 no.
#include<stdio.h>#include<conio.h>
void main()
{
int x=1;
clrscr();
while(x<10)
{
printf("%d\n",x);
x++;
}
getch();
}
do while loop-
- It is Exit Control loop.
- Initialization, Incrementation and Condition steps are on different line.
- It is also called Bottom Tested loop.
- do while condition is tested at bottom and body has to execute at least once.
- when do while loop has to be executed at least one time.
- do while loop syntax;
Initialization;
do{
................
................
Incrementation;
}while(condion);
- Ex-Using do while to print 1 to 10 no.
#include<stdio.h>
#include<conio.h>
void main()
{
int x=1;
do{
printf("%d\n",x);
x++;
}while(x<10);
getch();
}