We will learn this tutorial about Nested while loop in Java programming language
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.
Syntax
while(expression) { statement(s) while(expression) { statement(s) } }
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
Nested for loop in C++ language
Nested while loop in C++ language
Nested for loop in Java language
Nested while loop in Java 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
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…