Java While Loop

</>Coding Tamilan.← Java course & curriculum
Java Masterclass · Lesson 15 / 50 · English

Java While Loop

Repeat while the condition remains true.

Step-by-step explanationsRunnable JavaPractice + answer

Read this topic in தமிழ் ↗.

By the end of this lesson, you can…

  • Explain the key idea behind While Loop.
  • Read and run a complete example, then predict what changes.
  • Recognize the topic-specific mistake discussed below.
01 / The concept

Understand the idea

A while loop checks its condition before each iteration. Identify the starting value, the condition, and the statement that moves you toward stopping. A loop that never changes a condition-related value may run forever.

02 / Step by step

Explore the details

Java While Loop

A while loop keeps running a block of code as long as a condition stays true. The moment the condition becomes false, the loop stops.

Think of it like a vending machine: keep dispensing items while there are items left. When the stock hits zero, stop.

Syntax

Code anatomy / கோடின் விளக்கம்

CodeMeaning
whileKeyword that starts the loop
i < 5Condition checked before every iteration — must evaluate to true or false
) {Opening brace — loop body starts here
iCounter variable
++Update — without this the loop runs forever!
}Closing brace — loop body ends here

Countdown Example

Always update the variable inside the loop. If i++ is missing, the condition never becomes false and the loop runs forever — an infinite loop that freezes the program.

Java reference fragmentJAVA
// while loop
int i = 1;
while (i <= 5) {
    System.out.print(i + " ");  // 1 2 3 4 5
    i++;
}

// do-while — executes at least once
int n = 0;
do {
    System.out.println("Attempt: " + n);
    n++;
} while (n < 3);

// Sum using while
int sum = 0, num = 1;
while (num <= 10) { sum += num; num++; }
System.out.println("Sum 1-10: " + sum);  // 55
03 / Read and predict

A complete runnable example

This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.

Main.java · complete runnable exampleJAVA
public class Main {
    public static void main(String[] args) throws Exception {
        int attempt = 1;
        while (attempt <= 3) {
            System.out.println("Attempt " + attempt);
            attempt++;
        }
    }
}
Predict first. Then reveal the expected output / வெளியீடு
Attempt 1
Attempt 2
Attempt 3

The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.

04 / Learn by changing

Change it. Run it. Understand it.

Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.

Open Java editor ↗
After the editor loads, choose Load this lesson example, then press RUN.

Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.

05 / Your turn

Make it your own

Practice challenge

Start attempt at 4 without changing the condition. How many times does the body run?

Reveal the answer and reasoning / விடை

Zero times: the condition is false before the first iteration.

One mistake to avoid: Forgetting attempt++ here creates a non-terminating loop. Stop a stuck run rather than repeatedly starting more executions.

Your learning checkpoint

  • Explain the example without reading the output first.
  • Make the change requested in the practice task.
  • Describe why the result changed—or why compilation failed.

Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.

Coding Tamilan · Understand the idea. Try the code. Build the habit.