Nested Loop
Loop inside the loop is called nested loop There are three types of nested loop in c
Nested For
Nested While
Nested Do while
Nested For
Nested While
Nested Do while
Nested For
For inside the for is called nested for
Syntax:-
For(initialization;condition;inc/dec)
{
For(initialization;condition;inc/dec)
{
statement
}
}
Syntax:-
For(initialization;condition;inc/dec)
{
For(initialization;condition;inc/dec)
{
statement
}
}
*
**
***
****
*****
#include<stdio>.h
#include<conio>.h
void main()
{
int r,c;
clrscr();
for(r=1;r<=5;r++)
{
for(c=1;c<=r;c++)
{
printf("*");
}
printf("\n");
}
getch();
}
#include<conio>.h
void main()
{
int r,c;
clrscr();
for(r=1;r<=5;r++)
{
for(c=1;c<=r;c++)
{
printf("*");
}
printf("\n");
}
getch();
}
1
12
123
1234
12345
#include<stdio>.h
#include<conio>.h
void main()
{
int r,c;
clrscr();
for(r=1;r<=5;r++)
{
for(c=1;c<=r;c++)
{
printf("%d",c);
}
printf("\n");
}
getch();
}
#include<conio>.h
void main()
{
int r,c;
clrscr();
for(r=1;r<=5;r++)
{
for(c=1;c<=r;c++)
{
printf("%d",c);
}
printf("\n");
}
getch();
}
1
22
333
4444
55555
#include<stdio>.h
#include<conio>.h
void main()
{
int r,c;
clrscr();
for(r=1;r<=5;r++)
{
for(c=1;c<=r;c++)
{
printf("%d",r);
}
printf("\n");
}
getch();
}
#include<conio>.h
void main()
{
int r,c;
clrscr();
for(r=1;r<=5;r++)
{
for(c=1;c<=r;c++)
{
printf("%d",r);
}
printf("\n");
}
getch();
}
1
23
456
78910
1112131415
#include<stdio>.h
#include<conio>.h
void main()
{
int r,c,k=1;
clrscr();
for(r=1;r<=5;r++)
{
for(c=1;c<=r;c++)
{
printf("%d",k);
k++;
}
printf("\n");
}
getch();
}
>
#include<conio>.h
void main()
{
int r,c,k=1;
clrscr();
for(r=1;r<=5;r++)
{
for(c=1;c<=r;c++)
{
printf("%d",k);
k++;
}
printf("\n");
}
getch();
}
Nested While
While inside the While is called nested while
Syntax:-
while(condition)
{
while(condition)
{
statement
}
}
Syntax:-
while(condition)
{
while(condition)
{
statement
}
}
*
**
***
****
*****
#include<stdio>.h
#include<conio>.h
void main()
{
int r=1,c;
clrscr();
while(r<=5)
{
c=1;
while(c<=r)
{
printf("*");
c++;
}
printf("\n");
r++;
}
getch();
}
#include<conio>.h
void main()
{
int r=1,c;
clrscr();
while(r<=5)
{
c=1;
while(c<=r)
{
printf("*");
c++;
}
printf("\n");
r++;
}
getch();
}
1
12
123
1234
12345
#include<stdio>.h
#include<conio>.h
void main()
{
int r=1,c;
clrscr();
while(r<=5)
{
c=1;
while(c<=r)
{
printf("%d",c);
c++;
}
printf("\n");
r++;
}
getch();
}
#include<conio>.h
void main()
{
int r=1,c;
clrscr();
while(r<=5)
{
c=1;
while(c<=r)
{
printf("%d",c);
c++;
}
printf("\n");
r++;
}
getch();
}
1
22
333
4444
55555
#include<stdio>.h
#include<conio>.h
void main()
{
int r=1,c;
clrscr();
while(r<=5)
{
c=1;
while(c<=r)
{
printf("%d",r);
c++;
}
printf("\n");
r++;
}
getch();
}
#include<conio>.h
void main()
{
int r=1,c;
clrscr();
while(r<=5)
{
c=1;
while(c<=r)
{
printf("%d",r);
c++;
}
printf("\n");
r++;
}
getch();
}
1
23
456
78910
1112131415
#include<stdio>.h
#include<conio>.h
void main()
{
int r=1,c,k=1;
clrscr();
while(r<=5)
{
c=1;
while(c<=r)
{
printf("%d",k);
k++;
c++;
}
printf("\n");
r++;
}
getch();
}
#include<conio>.h
void main()
{
int r=1,c,k=1;
clrscr();
while(r<=5)
{
c=1;
while(c<=r)
{
printf("%d",k);
k++;
c++;
}
printf("\n");
r++;
}
getch();
}
Nested Do While
Do While inside the Do While is called nested Do while
Syntax:-
do
{
do
{
statement
}
while(condition);
}
while(condition);
Syntax:-
do
{
do
{
statement
}
while(condition);
}
while(condition);
*
**
***
****
*****
#include<stdio>.h
#include<conio>.h
void main()
{
int r=1,c;
clrscr();
do
{
c=1;
do
{
printf("*");
c++;
}
while(c<=r);
printf("\n");
r++;
}
while(r<=5);
getch();
}
#include<conio>.h
void main()
{
int r=1,c;
clrscr();
do
{
c=1;
do
{
printf("*");
c++;
}
while(c<=r);
printf("\n");
r++;
}
while(r<=5);
getch();
}
1
12
123
1234
12345
#include<stdio>.h
#include<conio>.h
void main()
{
int r=1,c;
clrscr();
do
{
c=1;
do
{
printf("%d",c);
c++;
}
while(c<=r);
printf("\n");
r++;
}
while(r<=5);
getch();
}
#include<conio>.h
void main()
{
int r=1,c;
clrscr();
do
{
c=1;
do
{
printf("%d",c);
c++;
}
while(c<=r);
printf("\n");
r++;
}
while(r<=5);
getch();
}
1
22
333
4444
55555
#include<stdio>.h
#include<conio>.h
void main()
{
int r=1,c;
clrscr();
do
{
c=1;
do
{
printf("%d",r);
c++;
}
while(c<=r);
printf("\n");
r++;
}
while(r<=5);
getch();
}
#include<conio>.h
void main()
{
int r=1,c;
clrscr();
do
{
c=1;
do
{
printf("%d",r);
c++;
}
while(c<=r);
printf("\n");
r++;
}
while(r<=5);
getch();
}
1
23
456
78910
1112131415
#include<stdio>.h
#include<conio>.h
void main()
{
int r=1,c,k=1;
clrscr();
do
{
c=1;
do
{
printf("%d",k);
c++;
k++
}
while(c<=r);
printf("\n");
r++;
}
while(r<=5);
getch();
}
#include<conio>.h
void main()
{
int r=1,c,k=1;
clrscr();
do
{
c=1;
do
{
printf("%d",k);
c++;
k++
}
while(c<=r);
printf("\n");
r++;
}
while(r<=5);
getch();
}
Break Statement in C
In a looping statement, the break command ends the loop and moves control to the next command outside the loop.
#include<stdio>.h
#include<conio>.h
void main()
{
int a;
clrscr();
for(a=1;a<=10;a++)
{
if(a==6)
{
break;
}
printf("%d\n",a);
}
getch();
}
#include<conio>.h
void main()
{
int a;
clrscr();
for(a=1;a<=10;a++)
{
if(a==6)
{
break;
}
printf("%d\n",a);
}
getch();
}
Continue Statement in C
The continue statement is used to skip the current iteration of any loop and bring the control to the beginning of the iteration.
#include<stdio>.h
#include<conio>.h
void main()
{
int a;
clrscr();
for(a=1;a<=10;a++)
{
if(a==6)
{
continue;
}
printf("%d\n",a);
}
getch();
}
#include<conio>.h
void main()
{
int a;
clrscr();
for(a=1;a<=10;a++)
{
if(a==6)
{
continue;
}
printf("%d\n",a);
}
getch();
}
Infinite loop and Exit Statement in C
An infinite loop is a sequence of instructions in a computer program which loops endlessly, either due to the loop having no terminating condition, having one that can never be met, or one that causes the loop to start over.
The EXIT statement exits the current iteration of a loop, either conditionally or unconditionally, and transfers control to the end of either the current loop or an enclosing labeled loop. An EXIT statement must be inside a LOOP statement.
#include<stdio>.h
#include<conio>.h
#include<stdlib>.h
void main()
{
int a,b,ch;
clrscr();
printf("Enter two nos=");
scanf("%d%d",&a,&b);
while(1)
{
printf("Enter 1 for sum\nEnter 2 for mul\nEnter 3 for sub\nEnter 4 for div\nEnter 5 for exit\nEnter your choice=");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("sum=%d\n",a+b);
break;
case 2:
printf("mul=%d\n",a*b);
break;
case 3:
printf("sub=%d\n",a-b);
break;
case 4:
printf("div=%d\n",a/b);
break;
case 5:
exit(0)
}
}
getch();
}
#include<conio>.h
#include<stdlib>.h
void main()
{
int a,b,ch;
clrscr();
printf("Enter two nos=");
scanf("%d%d",&a,&b);
while(1)
{
printf("Enter 1 for sum\nEnter 2 for mul\nEnter 3 for sub\nEnter 4 for div\nEnter 5 for exit\nEnter your choice=");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("sum=%d\n",a+b);
break;
case 2:
printf("mul=%d\n",a*b);
break;
case 3:
printf("sub=%d\n",a-b);
break;
case 4:
printf("div=%d\n",a/b);
break;
case 5:
exit(0)
}
}
getch();
}