Nested For Loops
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