nested while loop

Nested while loop in Java programming language

Nested while loop in Java programming language

We will learn this tutorial about Nested while loop in Java programming language

Nested while loop inJava language

Nested while loop

When a while loop exists inside the body of another while loop, it is known as nested while loop in Java. Initially, the outer loop executes once and the afterwards inner loop begins to execute. Execution of the inner loop continues until the condition of the inner loop is satisfied(until the test expression is false).

Once the Condition of the inner loop is satisfied, the flow of control comes out to the outer loop for next iteration.

Declaration

Syntax

while(expression)
           {
             statement(s)
                 while(expression)
                                {
                                 statement(s)
                               }
           }

Flow diagram for the nested while loop

Flowchart for the nested while loop

Example for nested while loop in Java

Example

class nestedwhile{
public static void main(String args[]){
int i=1;
    while(i<=3){
        System.out.println("\n"+i+" "+"outer loop executed only once\n");
    int j=1;
        while(j<=4)
        {
        System.out.println(j+" "+"inner loop executed until to completion");
            j++;  
        }
        i++;

    }
}
}

When we execute the above program, it produces the following results

1 outer loop executed only once

1 inner loop executed until to completion
2 inner loop executed until to completion
3 inner loop executed until to completion
4 inner loop executed until to completion

2 outer loop executed only once

1 inner loop executed until to completion
2 inner loop executed until to completion
3 inner loop executed until to completion
4 inner loop executed until to completion

3 outer loop executed only once

1 inner loop executed until to completion
2 inner loop executed until to completion
3 inner loop executed until to completion
4 inner loop executed until to completion

 

Program 1

Print rectangular number pattern using nested while loop in Java

class nestedwhile{
public static void main(String args[]){
int i=1,j=1;
    while(i<=10)
    {
        while(j<=10)
        {
        System.out.print(j);
            j++;  
        }
        i++;
        System.out.println("");
        j=1;
    }
}
}

 

When we execute the above program, it produces the following results

12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910

 

Program 2

Print rectangular star pattern using nested while loop in Java

class nestedwhile{
public static void main(String args[]){
int i=1,j=1;
    while(i<=10)
    {
        while(j<=10)
        {
        System.out.print("*");
            j++;  
        }
        i++;
        System.out.println("");
        j=1;
    }
}
}

 

When we executed above program, produces the following result

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

 

Program 3

Print triangle star pattern using nested while loop in Java

class nestedwhile{
public static void main(String args[]){
int i=1,j=1;
    while(i<=10)
    {
        while(j<=i)
        {
        System.out.print("*");
            j++;  
        }
        i++;
        System.out.println("");
        j=1;
    }
}
}

 

When we execute above program, produces the following result

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

 

Suggested for you

Java while loop

C while loop

C++ while loop

Python while loop

Java While keyword

 

 

See also

Floyd’s triangle number pattern in Java

 

 

for loop in C programming language with example
Nested while loop in C 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

PHP Star Triangle pattern program

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

4 weeks 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…

1 month 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