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

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

 

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

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

1 day ago

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