Java While Loop

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

By this, we can say Java while loop may compile zero or more time, and the syntax is

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

First, Javac tests the Java While loop condition. If the condition result is True, the statement(s) Jstatement 1 to Jstatement N inside it will run. If it is False, the Javac will get out of the curly brackets and complete the declarations outside. Curly braces are not required for a single-line while loop code in Java Programming. However, if we ignore them for multiple statements, the 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

While loop FLOW CHART

At the beginning of it, the While loop in Java programming tests the condition.

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

Let’s view one example of a While loop for a better conclusion.

Java While Loop example

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

import java.util.Scanner;

public class Sample {

	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 this is: %d ", sum);

	}

}

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

Java While Loop 1

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

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

First Iteration

sum = sum + number

sum = 0 + 7 ==> 7

Next, the 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 = 7 + 8 = 15

Third Iteration

From the Second Iteration, 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 will fail

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

Java Infinite While Loop example

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

package Test;

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

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

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

 
package Test;

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

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

1
2
3
4
5
6
7
8
9
10

Comments are closed.