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
For inside the for is called nested for
Syntax:-
For(initialization;condition;inc/dec)
{
For(initialization;condition;inc/dec)
{
statement
}
}
*
**
***
****
*****
#include<iostream>.h
#include<conio>.h
class aptech
{
public:
pattern()
{
int r,c;
for(r=1;r<=5;r++)
{
for(c=1;c<=r;c++)
{
cout<<"*";
}
cout<<endl;
}
}
};
void main()
{
aptech ap;
clrscr();
ap.pattern();
getch();
}
#include<conio>.h
class aptech
{
public:
pattern()
{
int r,c;
for(r=1;r<=5;r++)
{
for(c=1;c<=r;c++)
{
cout<<"*";
}
cout<<endl;
}
}
};
void main()
{
aptech ap;
clrscr();
ap.pattern();
getch();
}
1
12
123
1234
12345
#include<iostream>.h
#include<conio>.h
class aptech
{
public:
pattern()
{
int r,c;
for(r=1;r<=5;r++)
{
for(c=1;c<=r;c++)
{
cout<<c;
}
cout<<endl;
}
}
};
void main()
{
aptech ap;
clrscr();
ap.pattern();
getch();
}
#include<conio>.h
class aptech
{
public:
pattern()
{
int r,c;
for(r=1;r<=5;r++)
{
for(c=1;c<=r;c++)
{
cout<<c;
}
cout<<endl;
}
}
};
void main()
{
aptech ap;
clrscr();
ap.pattern();
getch();
}
1
22
333
4444
55555
#include<iostream>.h
#include<conio>.h
class aptech
{
public:
pattern()
{
int r,c;
for(r=1;r<=5;r++)
{
for(c=1;c<=r;c++)
{
cout<<r;
}
cout<<endl;
}
}
};
void main()
{
aptech ap;
clrscr();
ap.pattern();
getch();
}
#include<conio>.h
class aptech
{
public:
pattern()
{
int r,c;
for(r=1;r<=5;r++)
{
for(c=1;c<=r;c++)
{
cout<<r;
}
cout<<endl;
}
}
};
void main()
{
aptech ap;
clrscr();
ap.pattern();
getch();
}
1
23
456
78910
1112131415
#include<iostream>.h
#include<conio>.h
class aptech
{
public:
pattern()
{
int r,c,k=1;
for(r=1;r<=5;r++)
{
for(c=1;c<=r;c++)
{
cout<<k;
k++;
}
cout<<endl;
}
}
};
void main()
{
aptech ap;
clrscr();
ap.pattern();
getch();
}
#include<conio>.h
class aptech
{
public:
pattern()
{
int r,c,k=1;
for(r=1;r<=5;r++)
{
for(c=1;c<=r;c++)
{
cout<<k;
k++;
}
cout<<endl;
}
}
};
void main()
{
aptech ap;
clrscr();
ap.pattern();
getch();
}
Nested While
While inside the While is called nested while
Syntax:-
while(condition)
{
while(condition)
{
statement
}
}
*
**
***
****
*****
#include<iostream>.h
#include<conio>.h
class aptech
{
public:
pattern()
{
int r=1,c;
while(r<=5)
{
c=1;
while(c<=r)
{
cout<<"*";
c++;
}
cout<<endl;
r++;
}
}
};
void main()
{
aptech ap;
clrscr();
ap.pattern();
getch();
}
#include<conio>.h
class aptech
{
public:
pattern()
{
int r=1,c;
while(r<=5)
{
c=1;
while(c<=r)
{
cout<<"*";
c++;
}
cout<<endl;
r++;
}
}
};
void main()
{
aptech ap;
clrscr();
ap.pattern();
getch();
}
1
12
123
1234
12345
#include<iostream>.h
#include<conio>.h
class aptech
{
public:
pattern()
{
int r=1,c;
while(r<=5)
{
c=1;
while(c<=r)
{
cout<<c;
c++;
}
cout<<endl;
r++;
}
}
};
void main()
{
aptech ap;
clrscr();
ap.pattern();
getch();
}
#include<conio>.h
class aptech
{
public:
pattern()
{
int r=1,c;
while(r<=5)
{
c=1;
while(c<=r)
{
cout<<c;
c++;
}
cout<<endl;
r++;
}
}
};
void main()
{
aptech ap;
clrscr();
ap.pattern();
getch();
}
1
22
333
4444
55555
#include<iostream>.h
#include<conio>.h
class aptech
{
public:
pattern()
{
int r=1,c;
while(r<=5)
{
c=1;
while(c<=r)
{
cout<<r;
c++;
}
cout<<endl;
r++;
}
}
};
void main()
{
aptech ap;
clrscr();
ap.pattern();
getch();
}
#include<conio>.h
class aptech
{
public:
pattern()
{
int r=1,c;
while(r<=5)
{
c=1;
while(c<=r)
{
cout<<r;
c++;
}
cout<<endl;
r++;
}
}
};
void main()
{
aptech ap;
clrscr();
ap.pattern();
getch();
}
1
23
456
78910
1112131415
#include<iostream>.h
#include<conio>.h
class aptech
{
public:
pattern()
{
int r=1,c,k=1;
while(r<=5)
{
c=1;
while(c<=r)
{
cout<<k;
c++;
k++;
}
cout<<endl;
r++;
}
}
};
void main()
{
aptech ap;
clrscr();
ap.pattern();
getch();
}
#include<conio>.h
class aptech
{
public:
pattern()
{
int r=1,c,k=1;
while(r<=5)
{
c=1;
while(c<=r)
{
cout<<k;
c++;
k++;
}
cout<<endl;
r++;
}
}
};
void main()
{
aptech ap;
clrscr();
ap.pattern();
getch();
}
Nested Do While
Do While inside the Do While is called nested Do while
Syntax:-
do
{
do
{
statement
}
while(condition);
}
while(condition);
*
**
***
****
*****
#include<iostream>.h
#include<conio>.h
class aptech
{
public:
pattern()
{
int r=1,c;
do
{
c=1;
do
{
cout<<"*";
c++;
}
while(c<=r);
cout<<endl;
r++;
}
while(r<=5);
}
};
void main()
{
aptech ap;
clrscr();
ap.pattern();
getch();
}
#include<conio>.h
class aptech
{
public:
pattern()
{
int r=1,c;
do
{
c=1;
do
{
cout<<"*";
c++;
}
while(c<=r);
cout<<endl;
r++;
}
while(r<=5);
}
};
void main()
{
aptech ap;
clrscr();
ap.pattern();
getch();
}
1
12
123
1234
12345
#include<iostream>.h
#include<conio>.h
class aptech
{
public:
pattern()
{
int r=1,c;
do
{
c=1;
do
{
cout<<c;
c++;
}
while(c<=r);
cout<<endl;
r++;
}
while(r<=5);
}
};
void main()
{
aptech ap;
clrscr();
ap.pattern();
getch();
}
#include<conio>.h
class aptech
{
public:
pattern()
{
int r=1,c;
do
{
c=1;
do
{
cout<<c;
c++;
}
while(c<=r);
cout<<endl;
r++;
}
while(r<=5);
}
};
void main()
{
aptech ap;
clrscr();
ap.pattern();
getch();
}
1
22
333
4444
55555
#include<iostream>.h
#include<conio>.h
class aptech
{
public:
pattern()
{
int r=1,c;
do
{
c=1;
do
{
cout<<r;
c++;
}
while(c<=r);
cout<<endl;
r++;
}
while(r<=5);
}
};
void main()
{
aptech ap;
clrscr();
ap.pattern();
getch();
}
#include<conio>.h
class aptech
{
public:
pattern()
{
int r=1,c;
do
{
c=1;
do
{
cout<<r;
c++;
}
while(c<=r);
cout<<endl;
r++;
}
while(r<=5);
}
};
void main()
{
aptech ap;
clrscr();
ap.pattern();
getch();
}
1
23
456
78910
1112131415
#include<iostream>.h
#include<conio>.h
class aptech
{
public:
pattern()
{
int r=1,c,k=1;
do
{
c=1;
do
{
cout<<k;
c++;
k++;
}
while(c<=r);
cout<<endl;
r++;
}
while(r<=5);
}
};
void main()
{
aptech ap;
clrscr();
ap.pattern();
getch();
}
#include<conio>.h
class aptech
{
public:
pattern()
{
int r=1,c,k=1;
do
{
c=1;
do
{
cout<<k;
c++;
k++;
}
while(c<=r);
cout<<endl;
r++;
}
while(r<=5);
}
};
void main()
{
aptech ap;
clrscr();
ap.pattern();
getch();
}