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
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
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
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…