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. Java While loop start by verifying the condition, if it is true, the code within this 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 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 of it. Curly braces not required for a single line 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 Java While loop tests the condition.

  1. If the test results True, the code inside it will run.
  2. Next, we have to use Java Increment and Decrement Operators within this 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 test results are True.
  4. If the test results False, Javac will terminate it.

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.

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 number = 7. It means, total = 7 + 8 + 9 + 10 = 34

Java While Loop 1

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

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

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

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

Infinite While Loop example

Within this, if you skipped to increment or decrement the number, it will run for infinite times, also called as the Java infinite loop. Let me show you an example of Java 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
.....
....

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

Let’s put an increment operator (number++) inside of 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.