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.
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.
Nested for loop has two parts of the 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
Suggested for you
See also
floyd’s triangle Pattern printing using nested for loop in Java
floyd’s triangle Pattern printing using nested while loop in java
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…