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
* ** *** **** ***** ****** ******* ******** ********* **********
See also
Floyd’s triangle number pattern in Java
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…