loop in C
loop in C
Loop or iterative
-------------------------------------------------------------------------
- while loop
- do-while loop
- for loop
- nested loop
WHILE LOOP:
Syntax:
while(expression)
{
statement;
..............
..............
Example:
int n; // declaration
n = 1; // initializationo
printf("before start of the loop\n");
while(n<10)
{
printf("%d \t",n);//123456789
n++;
}
- DO-WHILE LOOP
Syntax:
do
{
statement;
..........
..........
}
while(expression);
- FOR LOOP
Syntax:
for(initialization; expression; increment /decrement)
{
statement;
.............
}
Example:
int n ;
for(n =10; n>=20; n--)
{
printf("count : %d\n",n);
}
Syntax:
Example:
int i,j;
for(i = 0; i<10; i++)
{
for(j = 0; j<=i; j++)
{
printf("%c",3);
}
printf("\n");
}
Output:
@
@@
@@@
@@@@
@@@@@
@@@@@@
@@@@@@@
@@@@@@@@
@@@@@@@@@
@@@@@@@@@@