Java While loop

The Article is about the use of Java while loop.

The Java while loop has two components, a condition and a statements. The statement may be a single statement or a block of statements, and the condition defines the condition that controls the loop. The condition may be any valid Boolean expression.

The loop repeats while the given condition is true. When the condition becomes false, the loops terminates and program control passes to the line immediately following the loop.

The while loop has a similar counterpart called the do while loop.

Syntax

Below is the general code for creating a while loop. The while loop first checks the condition, and then decides what to do. If the condition proves to be false, the instructions in the code block will not be executed.

while (condition) {
   //Instructions be executed
}

Example

A basic example of the Java while loop. Other supporting code was removed to improve readability and keep the code short.

int x = 0;

while (x < 5) {
    System.out.println(x);
    x = x + 1;
}
0
1
2
3
4

As shown above, the while loop printed out all the numbers from 0 to 4. The loop terminated once the value of x reached 5, before the output statement was even reached. Hence 5 was not included in the output.


Using multiple conditions

Using the and operator && we can connect two conditions together. Chaining two conditions together with && ensures the loop only runs when both conditions are true, and will terminate if any of them becomes false.

public class example {
	public static void main(String[] args) 	{		
		int x = 0;	
		int y = 0;
		
		while (x < 5 && y < 6) {
		    x = x + 1;
		    y = y + 2;		    
		    System.out.println("X: " + x + " " + "Y: " + y);  
		}	
	}
}
X: 1 Y: 2
X: 2 Y: 4
X: 3 Y: 6

The above loop stops after the value of y reaches 6, even though x was still less than 5.

You can use other operators like the or and xor to change things up. See the Java Operators guide to learn more.

Short hand While loop

If you want to make a quick and small while loop, you can do it all within one line using the following format.

while (condition) //statements;

Loop control statements

There are several loop control in Java, that as the name implies, can be used to manipulate the do while loop during it’s execution.

continue statement

The continue statement, when encountered will skip any remaining lines of code in the current iteration of the loop and skip to the beginning of the next iteration.

while (x < 5) {
   x = x + 1;
   if (x == 3) continue;
   System.out.println(x); 
}	
1
2
4
5

Since the continue statement is called when the value of x is equal to three, the System.out statement is skipped, and thus the value of x not printed to screen.

break statement

The break statement when called, terminates a loop during it’s execution. It is an effective way of forcing your loop to stop if some unexpected outcome has been encountered.

while (x < 5) {
   x = x + 1; 
   if (x == 3) break;
   System.out.println(x); 
}	
1
2

The above code does not make it past the value 2, since as soon as the value of x becomes three, the loop terminates.


This marks the end of the Java while loop article. Any suggestions or contributions for CodersLegacy are more than welcome. You can ask any relevant questions in the comments section below.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments