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

 

 

Similar post

for loop in C++ language

While loop in C++ language

Nested for loop in C++ language

Nested while loop in C++ language

New keyword in Java language

For loop in Java language

While loop in Java language

Nested for loop in Java language

Nested while loop in Java language

 

Suggested for you

Two dim Array in C++ 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

Two dim Array in C language

Three dim Array in C language

 

 

 

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.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago