← Back to Core Java Course | Chapter 4: Control Flow | Lesson 5 of 9

Java while Loop

A while loop repeats steps as long as a condition stays true, like stirring soup until it is hot. It checks the condition before every round.
Syntax
java
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

java
// 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++;
		}
	}
}

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

java
// 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--;
		}
	}
}

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

java
// 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;
			}
		}
	}
}

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

java
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
		}
	}
}

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

java
// 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;
		}
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting to change the loop variable, which makes an infinite loop.
  2. Using a condition that is false from the start, so the loop body never runs.
  3. Off-by-one errors, such as <= instead of <, which run one time too many.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.