Nested_For_Loops_Java_Class_9
Nested_For_Loops_Java_Class_9
Introduction:
- Commonly used for patterns, matrix operations, or other multi-level iterative tasks.
A nested for loop consists of an outer loop and one or more inner loops. Each time the outer loop
Syntax:
// Code to execute
System.out.print("*");
}
System.out.println();
Output:
**
***
****
*****
2. Rectangular Pattern:
System.out.print("*");
System.out.println();
Output:
*****
*****
*****
*****
}
System.out.println();
Output:
12
123
1234
12345
System.out.print("*");
System.out.println();
Output:
*****
****
***
**
if(i == 1 || i == 4 || j == 1 || j == 5)
System.out.print("*");
else
System.out.print(" ");
System.out.println();
Output:
*****
* *
* *
*****
6. Alphabet Triangle:
char ch = 'A';
ch++;
System.out.println();
Output:
BC
DEF
GHIJ
KLMNO
Key Points to Remember:
- Nested loops are loops inside other loops, useful for multi-level iterations.
- Each iteration of the outer loop triggers a full cycle of the inner loop.
- Patterns, matrices, and data structures can be easily handled with nested loops.