Loops
Loops
How it Works
The loops in C language are used to execute a block of code or a part of the program several times. In other words, it
iterates/repeat a code or group of code many times.
Or Looping means a group of statements are executed repeatedly, until some logical condition is satisfied.
1 : while loop
2 : do-while loop
3 : for loop
Syntax :
variable initialization ;
do{
statements ;
variable increment or decrement ;
}while (condition);
#include<stdio.h>
#include<conio.h>
void main() {
int a,i;
a=5; i=1;
do {
printf("%d\t",a*i);
i++;
}while(i <= 10);
getch();
}
Output
5 10 15 20 25 30 35 40 45 50
decrement)
{
Statements;
}
getch();
}
Output
1 2 3 4 5 6 7 8 9 10
If you don't initialize any variable, check condition and increment or decrement variable in for loop, it is known as
infinitive for loop. In other words, if you place 2 semicolons in for loop, it is known as infinitive for loop.
for(; ;)
{
printf("infinitive for loop example by javatpoint");
}
How all the three loops works? A do while loop will always
executed the code in the do {} i.e.
A for loop initially initiates a counter A while loop will always evaluate body of the loop block first and
variable (initializationexpression), the test-expression initially. It the then evaluates the condition. In
then it checks the test- test-expression this case also, the counter variable
expression, and executes the body of becomes true, then the body of the is initialized outside the body of
the loop if the test expression is true. loop will be executed. The the loop.
After executing the body of the loop, update
{ while (i<=10) do
Printf(“%d”,i); } { {
Printf(“%d”,i);
Printf(“%d”,i); ++i;
++i
}
}
while (i<=10)