Nested for loop in Java language

Nested forloop in Java language

In this tutorial, we will discuss the concept of Nested forloop in Java language.

When a forloop exists inside the body of another forloop, it is known as nested forloop in java

Syntex

for (initialization; boolean_expresion; increment or decrement){
    // statement(s) inside the body

for (initialization; boolean_expresion; increment or dicrement){

      // statement(s) inside the body

}
}

Here we can see, a for loop is inside the body another for loop

Flow diagram

Explanation of outer and inner loop



Example programs     

Program 1 

This program displays square number pattern using nested whileloop

class forloop
{
public static void main(String args[])
              {

for(int x=1;x<=10; x++)   //Outer for loop
           {
System.out.println();
for(int y=1;y<=10; y++)
                           {
                                 System.out.print(y);
                            } 
                }
           }

}

When you execute the above code, it produces the following result

Example



Program 2

This program displays square star pattern using nested whileloop

Example



When you execute above code , it produces the folloing result

**********
**********
**********
**********
**********
**********
**********
**********
**********
**********

Program 3

This program displays Floyd’s triangle number pattern using nested whileloop

Example

When you execute above code , it produces the following result

*
**
***
****
*****
******
*******
********
*********
**********

 

Similar post

If statement in C++

Karmehavannan

Recent Posts

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

11 months ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

11 months ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

11 months ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

11 months ago

Function/method to convert Celsius into Fahrenheit -Entered by user

Function/method to convert Celsius into Fahrenheit -Entered by user In this article, we will discuss…

11 months ago

Temperature conversion: Celsius into Fahrenheit using function or method

Temperature conversion: Celsius into Fahrenheit using a function or method In this article, we will…

11 months ago

This website uses cookies.