0% found this document useful (0 votes)
11 views

Nested Loop

The document explains nested loops, which are loops within loops, detailing their structure and types such as nested for, while, and do-while loops. It provides examples of generating various patterns using nested loops and discusses the use of break and continue statements in nested loops. Additionally, it includes a section on labeled break and continue statements for controlling loop execution.

Uploaded by

bosekunal.cyber
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Nested Loop

The document explains nested loops, which are loops within loops, detailing their structure and types such as nested for, while, and do-while loops. It provides examples of generating various patterns using nested loops and discusses the use of break and continue statements in nested loops. Additionally, it includes a section on labeled break and continue statements for controlling loop execution.

Uploaded by

bosekunal.cyber
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Nested Loop

Nested Loop: Loop inside another loop is called as Nested loop.


In nested loop the inner loop completes its entire iteration for each iteration of outer
loop.
If outer loop rotates for 5 times and inner loop rotates for 3 times, then for every
iteration of outer loop the inner loop will iterate for three times.
Total number of iteration in nested loop = Number of iteration of outer loop X Number
of iteration of inner loop.
Total number of iteration will be 5 X 3 = 15
Types of Nested loop:
1) Nested for
2) Nested while
3) Nested do – while
4) Mixed Nested loop
Nested for loop: When a for loop is present inside another for loop it is called as nested
for loop.
Syntax:
for(initialisation; condition; updation)
{
for(initialisation; condition; updation)
{
Statement of inner loop
}
Statement of outer loop
}
Eg:
for(int i = 1; i <= 5; i++)
{
for(int j = 1; j <= 3; j++)
{
System.out.print(j);
}
System.out.println();
}

When i = 1; i<=5 → True j= 1,2,3 123


When i= 2; i<=5 → True j = 1,2 ,3 123
When i=3; i<=5 → True j =1,2,3 123
When i=4; i <=5 →True j= 1,2,3 123
When i =5; i<=5 → True j = 1,2 ,3 123
Outer loop is used for rows whereas inner loop is used for columns.
Q1) Write a program to generate below patterns:
a)
1
12
123
1234
12345
Ans:
class Pattern1
{
public static void main()
{
for(int i = 1; i <= 5; i++)
{
for(int j = 1; j <= i; j++)
{
System.out.print(j);
}
System.out.println(); // to change the line
}
}
}

b)
1
22
333
4444
55555
Ans:
class Pattern2
{
public static void main()
{
for(int i = 1; i <= 5; i++)
{
for(int j = 1; j <= i; j++)
{
System.out.print(i);
}
System.out.println();
}
}
}

c)
1
10
101
1010
10101
Ans:
class Pattern3
{
public static void main()
{
for(int i = 1; i <= 5; i++)
{
for(int j = 1; j <= i; j++)
{
if(j%2 == 0)
{
System.out.print("0");
}
else
{
System.out.print("1");
}
}
System.out.println();
}
}
}

d)
12345
1234
123
12
1
Ans:
class Pattern4
{
public static void main()
{
for(int i = 5; i >= 1; i--)
{
for(int j = 1; j <= i; j++)
{
System.out.print(j);
}
System.out.println();
}
}
}

e)
54321
5432
543
54
5
Ans:
class Pattern5
{
public static void main()
{
for(int i = 1; i <= 5; i++)
{
for(int j = 5; j >= i; j--)
{
System.out.print(j);
}
System.out.println();
}
}
}

f) Floyd’s Triangle
1
23
456
7 8 9 10
11 12 13 14
Ans:
class Pattern6
{
public static void main()
{
int a = 1;
for(int i = 1; i <= 5; i++)
{
for(int j = 1; j <= i; j++)
{
System.out.print(a+ " ");
a = a+1;
}
System.out.println();
}
}
}
g)
1
33
555
7777
Ans:
class Pattern7
{
public static void main()
{
int a = 1;
for(int i = 1; i <= 4; i++)
{
for(int j = 1; j <= i; j++)
{
System.out.print(a);
}
a=a+2;
System.out.println();
}
}
}

Q2) Write a program to input a number and print all prime digits present in that
number.
N = 1234567
Ouput:2357
Ans:
import java.util.*;
class PrimeDigits
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n;
System.out.println("Enter a number");
n = sc.nextInt();
int d, count =0;
while(n != 0)
{
d = n%10;
if(d == 2 || d == 3 || d == 5 || d == 7)
{
System.out.print(d);
}
n = n/10;
}
}
}

Q3) Patterns Program:


a)
*****
****
***
**
*
Ans:
class Pattern10
{
public static void main(String args[])
{
for(int i = 5; i >= 1; i--)
{
for(int j = 1; j <= i; j++)
{
System.out.print("* ");
}
System.out.println();
}
}
}
b)

12345
2345
345
45
5
Ans:
class Pattern11
{
public static void main(String args[])
{
for(int i = 1; i <= 5; i++)
{
for(int j = i; j <= 5; j++)
{
System.out.print(j);
}
System.out.println();
}
}
}

c)
2
23
234
2345
23456
Ans:
class Pattern12
{
public static void main(String args[])
{
for(int i = 2;i<=6;i++)
{
for(int j = 2; j<=i; j++)
{
System.out.print(j);
}
System.out.println();
}
}

}
Output Questions:
1) What will be the output of the following code?

for(int a = 1; a <= 2; a=a+1)


{
for(int b = 1; b<=2; b++)
{
System.out.println(a+ “:” +b);
}
}
Ans:
1:1
1:2
2:1
2:2

2) What will be the output of the following code?

for(int a = 1; a <= 5; a = a+2)


{
for(int b= 1; b <= 2; b++)
{
System.out.println(a+ “*”+b);
}
}
Ans:
a=1 b=1 1*1
b=2 1*2
a=3 b=1 3*1
b=2 3*2
a=5 b=1 5*1
b=2 5*2

Output:
1*1
1*2
3*1
3*2
5*1
5*2

H.W:
Q1) Write a program to generate the below patterns:
a)
55555
4444
333
22
1
Ans:
class Pattern13
{
public static void main(String args[])
{
for(int i = 5; i >= 1; i--)
{
for(int j = 1; j <= i; j++)
{
System.out.print(i);
}
System.out.println();
}
}
}
b)
1234
1234
1234
1234
Ans:
class Pattern14
{
public static void main(String args[])
{
for(int i = 1; i <= 4; i++)
{
for(int j = 1; j <= 4; j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
c)
1212
1212
1212
1212
Ans:
class Pattern15
{
public static void main(String args[])
{
for(int i = 1; i <= 4; i++)
{
for(int j = 1; j <= 4; j++)
{
if(j%2 == 0)
{
System.out.print("2");
}
else
{
System.out.print("1");
}
}
System.out.println();
}
}
}
d)
1****
*2***
**3**
***4*
****5

i → 1 to 5
j → 1 to 5
Ans:
class Pattern16
{
public static void main(String args[])
{
for(int i = 1; i <= 5; i++)
{
for(int j = 1; j <= 5; j++)
{
if(i==j)
{
System.out.print(i);
}
else
{
System.out.print("*");
}
}
System.out.println();
}
}
}
e)
A
BC
DEF
GHIJ
Ans:
class Pattern17
{
public static void main(String args[])
{
char chr = 'A';
for(int i = 1; i <= 4; i++)
{
for(int j = 1; j <= i; j++)
{
System.out.print(chr);
chr++;
}
System.out.println();
}
}
}

Q2) Using switch case statement, write a menu driven program as per user’s choice.
(a)
(b)
A 11111
AB 2222
ABC 333
ABCD 44
ABCDE 5

Ans:
import java.util.*;
class MenuDriven1
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
char ch;
System.out.println("Input a. for character triangle and b. for number triangle");
ch = sc.next().charAt(0);
switch(ch)
{
case 'a':
for(char i = 'A'; i <= 'E'; i++)
{
for(char j = 'A'; j <= i; j++)
{
System.out.print(j);
}
System.out.println();
}
break;
case 'b':
for(int i = 1; i <= 5; i++)
{
for(int j = 5; j >= i; j--)
{
System.out.print(i);
}
System.out.println();
}
break;
default:
System.out.println("Invalid choice");

}
}
}
Nested while loop: A while inside another while loop is called as nested while loop.
Syntax:
Initialisation
while(condition)
{
Initialisation of inner loop
while(condition)
{
Body of inner loop
Updation
}
Body of outer loop
Updation
}

Eg:
Output:
int a = 1;
while(a < = 4) @@@
{ @@@
int b = 1;
while(b < = 3) @@@
{ @@@
System.out.print(“@ ”);
b++;
}
System.out.println();
a++;
}
Nested do-while loop: A do – while loop inside another do – while loop is called as
nested do – while loop.
Syntax:

initialization
do
{
Initialization of inner loop
do
{
Body of inner loop
Updation;
}
while(condition);
body of outer loop;
updation;
}
while(condition);
eg:
int a = 1;
Output:
do
{ @@@
int b = 1; @@@
do
{ @@@
System.out.print(“@ ”); @@@
b++;
}
while(b <= 3);
a++;
System.out.println();
}
while(a <= 4);

Use of break statement in Nested loop:


When break statement is used in nested loop it terminates loop in which it is used,
before completing the iterations.
When break statement is used in inner loop, it terminates the inner loop and iterates the
outer loop.
Eg: Use of break in inner loop.

for(int a = 1; a < =3; a++)


Output:
{
for(int b = 1; b < = 3; b++) 1
{ 1
if(b == 2)
1
break;
System.out.println(b);
}
}
When break statement is used in outer loop, it terminates the outer loop and control
comes out of looping structure.
Eg:
for (int a = 1; a <= 3; a++)
{ Output:
if (a == 2) 1
break; 2
for(int b = 1; b < = 3; b++) 3
{
System.out.println(b);
}
}
Use of continue statement in nested loop: When continue statement is used in nested
loop it skips or ignores the execution of the statements and moves for next iteration.
When continue statement is used in inner loop, it skips the execution of remaining
statements in inner loop and goes for next iteration of inner loop.
Eg:
for(int a = 1; a < = 3; a++)
Output:
{
for(int b = 1; b <= 3; b++) 13
{ 13
if(b == 2)
13
continue;
System.out.print(b+ “ “);
}
System.out.println();
}
When continue statement is used in outer loop, it skips the execution of the remaining
statements and moves for next iteration of outer loop.
Eg:
for(int a = 1; a <= 3; a++) Output:
{
123
if(a == 2)
continue; 123
for(int b = 1; b < = 3; b++)
{
System.out.print(b+ “ ”);
}
System.out.println();
}
Labelled Break Statement: Sometimes it is required stop the execution of outer loop
from the inner loop when the condition is satisfied, labelled break statement is used for
this purpose.
Eg:
outer:
for(int a = 1; a <= 3; a++)
{
for(int b = 1; b <= 3; b++)
{
if(b == 2)
break outer;
System.out.println(b);
}
}
Output: 1
Here outer loop will stop immediately once the value of b is 2.

Labelled continue statement: When labelled continue statement is executed from inner
loop then it skips the iteration of inner loop and moves for next iteration of outer loop.
Eg:
outer:
for(int a =1 ; a <= 3; a++) Output:
{
1
for(int b = 1; b <= 3; b++)
{ 1
if(b == 2) 1
continue outer;
System.out.println(b);
}
}
Here when continue outer is executed it goes for next iteration of outer loop, skipping
the execution of inner loop.

You might also like