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

929c9600-9710-4158-b8bc-9a8f3cdb1452_4_Nested Loop-Lesson 0

The document discusses the concept of nested loops in programming, explaining their structure and flow of control. It includes code snippets demonstrating nested loops, their outputs, and practical applications such as inputting student marks and generating patterns. Additionally, it outlines learning objectives related to understanding and implementing nested loops effectively.

Uploaded by

aspotkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

929c9600-9710-4158-b8bc-9a8f3cdb1452_4_Nested Loop-Lesson 0

The document discusses the concept of nested loops in programming, explaining their structure and flow of control. It includes code snippets demonstrating nested loops, their outputs, and practical applications such as inputting student marks and generating patterns. Additionally, it outlines learning objectives related to understanding and implementing nested loops effectively.

Uploaded by

aspotkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Predict the output for the given snippets-

1.

int weeks = 3;
int days = 7;
for (int i = 1; i <= weeks; ++i)
{
System.out.println("Week: " + i);
for (int j = 1; j <= days; ++j)
{
System.out.println(" Day: " + j);
}
}
int rows = 5;
for (int i = 1; i <= rows; ++i)
{
for (int j = 1; j <= i; ++j)
{
System.out.print(j + " ");
}
System.out.println("");
}
for (int row = 1; row <= 5; row++) {
System.out.print("*");
for (int col = 1; col < 5; col++) {
System.out.print(" *");
}
System.out.println();
}
Week: 1
Day: 1
Day: 2
Day: 3
Day: 4
Day: 5
Day: 6
Day: 7
Week: 2
Day: 1
Day: 2
Day: 3
Day: 4
Day: 5
Day: 6
Day: 7
Week: 3
Day: 1
Day: 2
Day: 3
Day: 4
Day: 5
Day: 6
Day: 7
1
1 2
1 23
1 234
1 2345
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
 When a loop contains another loop in its body, then it is said
to be a Nested Loop. Learning to use the Nested loop is
important because there are a wide range of programming
scenarios that require the application of a nested loop.

 Describe the concept & flow of control in nested loop statements


 Identify when a nested loop is appropriate for a problem
 Implement Nested loops in programs
 Predict output for the given code snippets
At the end of this lesson, you should be able to

 Describe the concept & flow of control in nested loop


statements

 Predict the output of the given code snippets

 Write the program code to generate the given pattern

 Write a program using a Nested loop


 When a loop contains another loop in its body,
then it is said to be a Nested Loop.

 In a nested loop, the outer loop determines the


number of times the inner loop runs.

 The outer loop starts first, followed by the inner


loop.

 For each iteration of the outer loop, the inner


loop runs for a specified number of times,
terminates first and then the outer loop takes
over.
 The inner or outer loop can be any
type: while, do while, or for.

 For example, the inner loop can be


a while loop and the outer loop can be
a for loop.

 The inner and the outer loops can be of the


same kind also.
int x=1; //Program to enter marks of students
do // in 5 sections and each section
{ // consisting of 30 students
for(int i=1;i<=30;i++)
{
Write the statements to input the names & marks
of 30 students in math
}//for
/**Display the average math mark, the highest
and the lowest mark in math for that section
and then continue for the next class.*/
} while(++x<=5);
class nested_studmarks {
public static void main() {
int x=1;
do {
String y;
int i, sum=0,z, high=0, low=100;
System.out.println("Section: "+x);
for(i=1;i<=30;i++) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter your name");
y=sc.next();
System.out.println("Enter marks in maths");
z=sc.nextInt();
if (z > high)
high = z;
if (z < low)
low = z;
sum+=z;
} //for
float avg=(float)sum/i;
System.out.println("Average: "+avg);
System.out.println("Sum: "+sum);
System.out.println("Highest marks:"+h);
System.out.println("Lowest marks:"+l);
} while(++x<=5);
} //main
}//class
for(num2 = 0; num2 <= 3; num2++)
{
for(num1 = 0; num1 <= 2; num1++)
{
System.out.println(num2 + " " + num1);
}
}
How many times the inner loop executes in the
above snippet?
Give the dry run
 If a “break” statement appears in a nested-
loop, then it causes an exit from the very
loop it appears in. In other words it exits out
of the inner loop only. In order to exit out of
the outer loop, another break statement
needs to be given.

 Similarly the “continue” statement, continues


with the iteration in the loop it appears in. For
example, if it is in the inner loop, then it
continues with the iteration in the inner loop.
1
2 2
3 33
4 444
5 5555
for(x=1; x<=5;x++)
{
for(y=1;y<=x;y++)//no. of elements in the column depends on
the row no. eg 2nd row so 2 elements in the column, outer loop variable
printed
{
System.out.print(x+” “);
}
System.out.println();
}
 Write a menu driven program that displays the
list of options shown below, allows the user to
enter an option and execute the operation based
on user’s choice. The menu should be displayed
repeatedly till the user chooses exit.
 Use do.. While and switch case constructs to
accomplish the task.
 MENU
◦ 1 Display the Factors of any given number
◦ 2. Display the Factorial of any given number
◦ 3. Accept N numbers and display their average
◦ 4. Exit

class numbers {
public static void main() {
int a=1;
do {
System.out.println("Enter your choice: 1/2/3/4");
System.out.println("1.Display the Factors of any given number");
System.out.println("2.Display the Factorial of any given number");
System.out.println("3.Accept N numbers and display their average");
System.out.println("4.Exit");
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
switch(x) {
case 1:
System.out.println("Enter the number whose factors you want:");
int y=sc.nextInt();
int i;
for(i=1;i<=y;i++) {
if(y%i==0)
System.out.println("Factors of "+y+"are:"+ i);
} //for
break;
case 2:
System.out.println("Enter the number whose factorial you want:");
long s=sc.nextLong();
long j=1;
for(long t=1;t<=s;t++) {
j=j*t;
}
System.out.println("Factorial of "+s+"is:"+j);
break;

case 3:
System.out.println("Enter the ending number:");
int n=sc.nextInt();
int sum=0;
float avg;
int u;
for(u=1;u<=n;u++)
{
sum+=u;
}
avg=(float)sum/u;
System.out.println("Average of " + n+ "numbers is:" +avg);
break;
case 4:
System.exit(0);
break;

default:
System.out.println("Invalid input");
}//switch
}while(true);
}//main
}//class
CLOSURE

Let us revisit the success criteria and check


if we understood the topic.

Give a if you agree that the SC have been met or


if not.

You might also like