Nested for in Java programming language
- Home
- nested for
- Nested for in Java programming language
- On
- By
- 0 Comment
- Categories: nested for
Nested for in Java programming language
Nested for in Java programming language
We will learn in this tutorial about Nested for in Java programming language
If the for loop is inside the body of another for loop, it is known as nested for loop in Java language.
Declaration
Syntax
for(initialization; test_expression; updating_statement){ //codes inside the body of outer loop for(initialization; test_expression; updating_statement){ //codes inside the body of outer loop } }
Here, there are two for loops. An outer for loop exists inside a for loop called as inner for loop.
Explanation of nested for loop
Flow diagram for nested for loop
How works nested for loop
Nested for loop has two parts of the loop
- Inner for loop
- Outer for loop
Outer for loop executes only once when its test expression is true. Then, the flow of control moves to inner loop iteration and inner loop executes until its test expression is false.
When the test expression of the inner loop is false, the flow of control skips the execution of the inner loop and come out to execute the outer loop again. when the test expression of the outer loop is false, the flow of control skips execution and exit from the loop control.
Example
class nested_forworks { public static void main(String args[]) { for(int i=1; i<=3; i++){ System.out.println("\n"+i+" Outer loop is executed only once"); for(int j=1; j<=4; j++){ System.out.println(j+" Inner loop is executed until to complition"); } } } }
When we executed above program, produces the following result
1 Outer loop is executed only once 1 Inner loop is executed until to completion 2 Inner loop is executed until to completion 3 Inner loop is executed until to completion 4 Inner loop is executed until to completion 2 Outer loop is executed only once 1 Inner loop is executed until to completion 2 Inner loop is executed until to completion 3 Inner loop is executed until to completion 4 Inner loop is executed until to completion 3 Outer loop is executed only once 1 Inner loop is executed until to completion 2 Inner loop is executed until to completion 3 Inner loop is executed until to completion 4 Inner loop is executed until to completion 4 Outer loop is executed only once 1 Inner loop is executed until to completion 2 Inner loop is executed until to completion 3 Inner loop is executed until to completion 4 Inner loop is executed until to completion
program 1
Print rectangle number pattern using nested for loop in Java
class nested_forpat { public static void main(String args[]) { for(int i=1; i<=10; i++){ System.out.println(); for(int j=1; j<=10; j++){ System.out.print(j); } } } }
When we executed above program, produces the following result
12345678910 12345678910 12345678910 12345678910 12345678910 12345678910 12345678910 12345678910 12345678910 12345678910
program 2
Print rectangle star pattern usingĀ for loop in Java
class nested_forpat { public static void main(String args[]) { for(int i=1; i<=10; i++){ System.out.println(); for(int j=1; j<=10; j++){ System.out.print("*"); } } } }
When we executed above program, produces the following result
********** ********** ********** ********** ********** ********** ********** ********** ********** **********
Program 3
Print triangle number pattern usingĀ for loop in Java
class nested_forpat { public static void main(String args[]) { for(int i=1; i<=10; i++){ System.out.println(); for(int j=1; j<=i; j++){ System.out.print(j); } } } }
When we executed above program, produces the following result
1 12 123 1234 12345 123456 1234567 12345678 123456789 12345678910
Similar post
Nested for loop in C++ language
Nested while loop in C++ language
Nested for loop in Java language
Nested while loop in Java language
Suggested for you
Three dim Array in C++ language
Single dim Array in Java language
Two dim Array in Java language
Three dim Array in Java language
Single dim Array in C language