You are on page 1of 15

Programming for Problem Solving

BCAC101

Mr. Subhas Chandra Nath


Assistant Professor
Department of Computer
Application

1 Lecture :: 15
Contents

 Nested Loop

 Nested for Loop

 Nested while Loop

 Nested do-while Loop

 Hands On Example
2
Nested Loop

 If a loop exists inside the body of another loop, it's called a nested

loop.

 Any number of loops can be defined inside another loop, i.e., there

is no restriction for defining any number of loops.

 The nesting level can be defined at n times.

 You can define any type of loop inside another loop; for example,

you can define 'while' loop inside a 'for' loop.


3
Continue…

Syntax of Nested loop:


Outer_loop
{
Inner_loop
{
// inner loop statements.
}
// outer loop statements.
4 }
Nested for Loop

The nested for loop means any type of loop which is defined inside
the 'for' loop.

for (initialization; condition; update)


{
for(initialization; condition; update)
{
// inner loop statements.
}
// outer loop statements.
5 }
Continue…

Example1: Print the pattern using


nested for loop. scanf(“%d”,&r);
1 for(i=1;i<=r;i++)
23 {
456 for(k=1;k<=i;k++)
#include<stdio.h> printf("%3d",m++);
#include<conio.h> printf("\n");
void main() } Output
{ getch();
int r,i,k,m=1; }
clrscr();
printf("Enter the number of row:");
6
Continue…

Example2: Print the pattern using


nested for loop. for(i=1;i<=r;i++)
1 {
23 for(j=1;j<=r-i;j++)
456 printf(" ");
#include<stdio.h> for(k=1;k<=i;k++)
#include<conio.h> printf("%3d",m++);
void main() printf("\n"); Output
{ }
int r,i,j,k,m=1; getch();
clrscr(); }
printf("Enter the number of row:");
7
scanf(“%d”,&r);
Nested while Loop

The nested while loop means any type of loop which is defined
inside the ‘while' loop.
while(condition)
{
while(condition)
{
// inner loop statements.
}
// outer loop statements.
8 }
Continue…

Example3: Print the pattern using while(i<=r) {


nested while loop. k=1;
1 while(k<=i) {
printf("%3d",i);
22
k++;
333
}
#include<stdio.h>
i++;
#include<conio.h>
printf("\n");
void main() Output
}
{
getch();
int r,i=1,k;
}
clrscr();
printf("Enter the number of row:");
9 scanf("%d",&r);
Continue…

Example4: Print the pattern using while(i<=r) {


nested while loop. j=1;
1 while(j<=r-i) {
printf(“ “);
22
j++; }
333
k=1;
#include<stdio.h>
while(k<=i) {
#include<conio.h>
printf("%3d",i);
void main()
k++; } Output
{
i++;
int r,i=1,j,k;
printf("\n");
clrscr();
}
printf("Enter the number of row:");
getch();
10 scanf("%d",&r);
}
Nested do-while Loop

The nested do-while loop means any type of loop which is


defined inside the ‘do-while' loop.
do
{
do
{
// inner loop statements.
} while(condition);
// outer loop statements.
11 }while(condition);
Continue…

Example5: Print the pattern using do {


nested do-while loop. j=1;

* do {
printf(" * ");
**
j++;
***
}while(j<=i);
#include<stdio.h>
i++;
#include<conio.h>
printf("\n");
void main() Output
}while(i<=r);
{
getch();
int r,i=1,j;
}
clrscr();
printf("\nEnter the row:");
12 scanf("%d",&r);
Continue…

Example6: Print the pattern using do {


nested do-while loop. printf(" ");

* j++;
}while(j<=r-i+1);
**
k=1;
***
do {
#include<stdio.h>
printf(" * ");
#include<conio.h>
k++; Output
void main() {
}while(k<=i);
int r,i=1,j,k;
i++;
clrscr();
printf("\n");
printf("\nEnter the row:");
}while(i<=r);
scanf("%d",&r);
getch();
13 do {
}
j=1;
Hands On Example

Program1: Write a C program to print the following


pattern. 1
234
56789

Program 2: Write a C program to print hollow triangle


with # pattern. #
# #
# #
14 #######
Thank You.

Any clarification contact me through e-mail


scn.assignment@gmail.com

15

You might also like