Java while Loop
In this page:
while (condition) {
// loop body
// update variable
}
Simple while Loop
A while loop checks its condition immediately, before running the body even once -- if the condition starts out false, the loop body never executes at all.
Example: Simple while Loop
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare i and set it to 0
int i = 0;
// Repeat while i < 3 is true
while (i < 3) {
// Print a line to the console
System.out.println(i);
i++;
}
}
}
Login to try C/C++/Java/PHP code in the editor
Decrementing Loop Counters
Subtracting from a counter each pass through the loop, like count--, lets you count down toward zero (or any target) instead of the more common upward counting pattern.
Example: Decrementing Loop Counters
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare count and set it to 5
int count = 5;
// Repeat while count > 0 is true
while (count > 0) {
// Print a line to the console
System.out.println(count);
count--;
}
}
}
Login to try C/C++/Java/PHP code in the editor
Loops with Boolean Flags
A boolean flag variable in the condition, like while (running), lets code inside the loop body itself decide when to stop by setting that flag to false, which is useful when the stopping condition is complex or event-driven.
Example: Loops with Boolean Flags
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare running and set it to true
boolean running = true;
// Declare i and set it to 0
int i = 0;
// Repeat while running is true
while (running) {
// Print a line to the console
System.out.println(i);
i++;
// Check whether i == 3
if (i == 3) {
running = false;
}
}
}
}
Login to try C/C++/Java/PHP code in the editor
Avoiding Infinite Loops
If nothing inside the loop body ever changes the value(s) the condition depends on, the condition stays true forever and the loop runs indefinitely -- always double-check that your loop variable is actually being updated.
Example: Avoiding Infinite Loops
public class Main {
public static void main(String[] args) {
int i = 0;
while (i < 3) {
System.out.println(i);
i++; // removing this line would make the loop run forever
}
}
}
Login to try C/C++/Java/PHP code in the editor
Mathematical Growth Loops
Multiplying or dividing a loop variable each iteration (rather than adding or subtracting a fixed amount) produces exponential growth or decay, useful for algorithms like binary search that need logarithmic-scale steps.
Example: Mathematical Growth Loops
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare value and set it to 1
int value = 1;
// Repeat while value < 100 is true
while (value < 100) {
// Print a line to the console
System.out.println(value);
value *= 2;
}
}
}
Login to try C/C++/Java/PHP code in the editor
- Forgetting to change the loop variable, which makes an infinite loop.
- Using a condition that is
falsefrom the start, so the loop body never runs. - Off-by-one errors, such as
<=instead of<, which run one time too many.
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: