0% found this document useful (0 votes)
6 views12 pages

Nested For Loops

The document explains nested loops in programming, specifically focusing on nested for loops where an inner loop runs multiple times within an outer loop. It includes rules for creating patterns using nested loops and provides examples of Java code to illustrate these concepts. Additionally, it discusses the use of 'break' and 'continue' statements within nested loops to control the flow of execution.

Uploaded by

Pratyusha ig
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)
6 views12 pages

Nested For Loops

The document explains nested loops in programming, specifically focusing on nested for loops where an inner loop runs multiple times within an outer loop. It includes rules for creating patterns using nested loops and provides examples of Java code to illustrate these concepts. Additionally, it discusses the use of 'break' and 'continue' statements within nested loops to control the flow of execution.

Uploaded by

Pratyusha ig
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/ 12

NESTED FOR LOOPS

NESTED LOOPS
• A nested loop means a loop statement inside another loop
statement.
• When a loop is nested inside another loop, the inner loop runs
many times inside the outer loop. In each iteration of the outer loop,
the inner loop will be re-started.
• The inner loop must finish all of its iterations before the outer loop
can continue to its next iteration.
Example – Nested FOR Loop
Rules for making patterns
1. Count total number of rows and columns
2. Execute Outerloop for rows and inner loop for Columns
3. Print Outer loop variable when we have same values in rows
like –
1111
2 2 2 2 and set outer loop first then inner loop
4. Print Inner loop variable when we have different values in
rows like –
12345
Questions
class Sample
EXAMPLE
{
int i, j; Output
public static void main(String args[])
*****
{
for(int i=1 ; i<= 5 ; i++) *****
{
*****
for(j=1; j<= i ; j++)
System.out.println(“ * ”); *****
}
System.out.println();
}
}
Questions based on Nested For loop
Write a program in Java to
display the following pattern:
1
*
22 * *
333 * * *
4444 * * * *
55555 * * * * *
Nested For loop using Break statement
Nested For loop using Continue statement

• Java continue statement is used to skip the current iteration of a loop.


• Continue statement in java can be used with for , while and do-while
loop.
• After the continue statement, the program moves to the end of the loop.
public class Sample {
{
public static void main(String args[])
{
for(int i=1; i<=4 ; i++)
{
for(int j=1; j<=3 ; j++)
if(i==4 && j==3)
//Continue statement in inner loop to skip the execution when i==3
and j==2
continue;
}
System.out.println(i+ “ * ” + j);
}
}
}

You might also like