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.

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

Explanation of nested for loop

Flow diagram for nested for loop

Flow diagram for nested for loop

 

How works nested for loop

Nested for loop has two parts of the loop

  1. Inner for loop
  2. 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

 

Suggested for you

for loop in Java language

for loop in C language

for loop in C++ language

for loop in Python

 

See also

floyd’s triangle Pattern printing using nested for loop in Java

floyd’s triangle Pattern printing using nested while loop in java

 

for loop in C programming language with example
Nested for loop in Python programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Share
Published by
Karmehavannan

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

1 month ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

9 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

9 months ago