Java While Loop

The Java while loop is to iterate a code block for a given number of times till the condition inside a loop is False. Java While loop start by verifying the condition, if it is true, the code within the while loop will run. If the condition is false, the Java while loop will not run at least once.

By this, we can say, Java while loop may compile zero or more time. The Java While loop syntax is

While ( JCondition )  {
    Jstatement 1;
    Jstatement 2;
    ………….
    Jstatement N;
}
// This statement is from outside of the While Loop

First, Javac tests the Java While loop condition. If the condition result is True, the statement(s)  Jstatement 1 to Jstatement N inside the while loop will run. If it is False, the Javac will get out of the loop and complete the declarations outside the while loop. Curly braces not required for a single line code in Java Programming. However, if we ignore them for multiple statements, Javac will execute the first line only. It is safe to utilize braces all the time

Flow Chart of a Java While loop

The following picture presents the Java While Loop flow chart

Java While Loop FLOW CHART

At the beginning of the loop, the Java While loop tests the condition.

  1. If the test results True, the code inside the While loop will run.
  2. Next, we have to use Java Increment and Decrement Operators within the while loop to raise or lower the value.
  3. After the value incremented, again, Javac will test the condition. Javac will repeat the process as long as the while loop test results are True.
  4. If the test results False, Javac will terminate from the While loop

Let’s view one example of While loop in java programming for a better conclusion

Java While Loop example

This program for Java while loop lets the user insert an integer value under 10. Next, the Javac compiler finds the sum of those numbers up to 10.

// Java While Loop example

package Loops;

import java.util.Scanner;

public class WhileLoop {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int number, sum = 0;
		sc = new Scanner(System.in);	
		
		System.out.println("\n Please Enter the any integer Value below 10: ");
		number = sc.nextInt();
		
		while (number <= 10)  {
			sum = sum + number;
			number++;
		}
		System.out.format(" Sum of the Numbers From the While  Loop is: %d ", sum);
	}
}

We are going to enter number = 7. It means, total = 7 + 8 + 9 + 10 = 34

Java While Loop 1

We used the While loop, and the Condition inside the While loop will make sure that the given number is less than or equal to 10.

while (number <= 10)  {
	sum = sum + number;
	number++;
}

In this while loop example, User given values: Number = 7. Next, we initialized the sum = 0

Java First Iteration

sum = sum + number

sum = 0 + 7 ==> 7

Next, number will be incremented by 1 (number ++). Refer Increment & Decrement Operators article in Java to understand this ++ notation.

Second Iteration

Within the first Iteration, both Number and sum changed as Number = 8 and sum = 7

sum = sum + number

sum = 7 + 8 ==> 15

Third Iteration

From the Second Iteration, both Number and sum have changed as Number = 9 and sum = 15

sum = 15 + 9 ==> 24

Fourth Iteration

Within the third Iteration of the Java while loop, the values of both Number and sum changed as Number = 10 and sum = 24

sum = 24 + 10 ==> 34

Next, the number will increment by 1

Here Number = 11. So, the condition present in the while loop will fail

Last System.out.format statement will print the number of digits present in the given number as output.

System.out.format(" Sum of the Numbers From the While  Loop is: %d ", sum);

Infinite While Loop example

Within the While loop, if you skipped to increment or decrement the number, While loop will run for infinite times, also called an infinite loop. Let me show you an example of Java Infinite While loop

// Infinite While Loop in Java Programming example 
 
package Loops;

public class InfiniteWhile {
	public static void main(String[] args) {
		int number = 1;
		
		while (number <= 10)  {
			System.out.format("Numbers From While Loop = %d ", number);
		}
	}
}
Numbers From While Loop = 1
Numbers From While Loop = 1
Numbers From While Loop = 1
Numbers From While Loop = 1
Numbers From While Loop = 1
Numbers From While Loop = 1
Numbers From While Loop = 1
.....
....

Here, in this Java infinite While loop example, the number is forever 1, and it is ever less than ten. So, the while loop will go on executing the statement infinite times.

Let’s put an increment operator (number++) inside the while loop of the preceding example.

// Infinite While Loop in Java Programming example 
 
package Loops;

public class InfiniteWhile {
	public static void main(String[] args) {
		int number = 1;
		
		while (number <= 10)  {
			System.out.format("Numbers From While Loop = %d\n", number);
			number++;
		}
	}
}

Now, when it equals 10, the condition will fail. The output of this while loop example

Numbers From While Loop = 1
Numbers From While Loop = 2
Numbers From While Loop = 3
Numbers From While Loop = 4
Numbers From While Loop = 5
Numbers From While Loop = 6
Numbers From While Loop = 7
Numbers From While Loop = 8
Numbers From While Loop = 9
Numbers From While Loop = 10

Comments are closed.