Loading...

While Loops in Java: Repeating Until the Job Is Done

A Simple Thought

Sometimes you don’t know how many times something should repeat.
Imagine trying to guess a secret number. You keep guessing until you get it right, but you don’t know in advance how many tries it will take.

Computers often face this same kind of problem.
They can keep repeating a task as long as a certain condition is true. In Java, this is done with something called a while loop.

In this lesson, you’ll learn how to make your program keep going until the job is finished.

The Project: Countdown Timer

You will create a simple program that counts down from 10 to 1 and then prints “Liftoff!”.
The while loop will repeat until the countdown reaches zero.

Your output should look like this:

text
14 lines
|
33/ 500 tokens
1
2
3
4
5
6
7
8
9
10
11
12
13
14
========================
   COUNTDOWN TIMER
   10
   9
   8
   7
   6
   5
   4
   3
   2
   1
   Liftoff!
========================

The program will automatically count down using a while loop.

The Concept: The While Loop

A while loop is used when you want something to keep happening until a certain condition becomes false.
It looks like this:

java
3 lines
|
11/ 500 tokens
1
2
3
while (condition) {
    // repeated code
}
Code Tools

The condition is checked before every loop. If it’s true, the code inside runs again. If it becomes false, the loop stops.

For example:

java
5 lines
|
24/ 500 tokens
1
2
3
4
5
int count = 1;
while (count <= 5) {
    System.out.println("Number: " + count);
    count++;
}
Code Tools

This loop prints numbers from 1 to 5. Each time it runs, count increases by one. When count becomes 6, the condition is no longer true, so the loop stops.

Why This Matters

While loops make your programs flexible. You can repeat a task without knowing in advance how many times it needs to run.

Think about how useful that is:

  • A login system keeps asking for a password until it’s correct.
  • A game keeps running until the player loses.
  • A robot keeps moving forward until it reaches an obstacle.

These situations don’t have a fixed number of steps, so a while loop is the perfect tool.

Let’s Build the Countdown

Here’s the complete program for the countdown timer:

java
17 lines
|
106/ 500 tokens
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Countdown {
    public static void main(String[] args) {

        int number = 10;

        System.out.println("========================");
        System.out.println("   COUNTDOWN TIMER");

        while (number >= 1) {
            System.out.println("   " + number);
            number--;
        }

        System.out.println("   Liftoff!");
        System.out.println("========================");
    }
}
Code Tools

When you run this program, it will print the numbers in reverse order.
The variable number starts at 10, and each time the loop runs, it decreases by one until it reaches zero.
When the condition is no longer true, the loop ends.

How It Works

  1. The variable number starts at 10.
  2. The while loop checks if number is greater than or equal to 1.
  3. If true, the current value of number is printed.
  4. The line number-- reduces the value by 1 each time.
  5. Once number becomes 0, the condition is false, and the loop ends.

This is how Java repeats actions only while they are needed.

Learn Together with AI

If you’re using an AI Copilot, you can describe what you want to make and let it write the first version of your program.

Try this prompt:
“Write a Java program that counts down from 10 to 1 using a while loop and prints ‘Liftoff!’ at the end.”

Once you have it working, you can ask for improvements:

  • “Change it to start from 20.”
  • “Add a short message after every number.”
  • “Make it count down in steps of 2.”

The AI will adjust the code instantly, helping you experiment and learn faster.

Practice Challenge

Now it’s your turn.
Create a program that keeps asking the user to enter the password until they type the correct one.
The correct password should be “Java123”.

Your output should look like this:

text
9 lines
|
49/ 500 tokens
1
2
3
4
5
6
7
8
9
========================
   LOGIN SYSTEM
   Enter password: hello
   Access Denied
   Enter password: 12345
   Access Denied
   Enter password: Java123
   Access Granted
========================

The program should stop only when the correct password is entered.

Example Solution

java
24 lines
|
167/ 500 tokens
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.Scanner;

public class PasswordCheck {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        String password = "";

        System.out.println("========================");
        System.out.println("   LOGIN SYSTEM");

        while (!password.equals("Java123")) {
            System.out.print("   Enter password: ");
            password = input.nextLine();

            if (!password.equals("Java123")) {
                System.out.println("   Access Denied");
            }
        }

        System.out.println("   Access Granted");
        System.out.println("========================");
    }
}
Code Tools

When you run this program, it will keep asking for the password.
Once “Java123” is entered, the loop ends and the access message appears.
This is the power of while loops, they repeat until the condition is satisfied.

Coming Up Next

You now know how to make Java keep repeating tasks until a condition changes.
Next, you’ll take this idea even further. You’ll learn how to use loops inside loops to create shapes, patterns, and designs using text.