Loading...

Java while Loops

Put the whole number 1 under the name k before any loop. Write while (k <= 3), and inside the braces print k and then store k + 1 back into k. The screen should show three lines: 1, then 2, then 3. The start lives above the while. The test lives in the parentheses. The step lives in the braces. That split is what this page teaches.

Start outside, test in while, step inside

Create a small program file named Pulse.java. The public class on the first line must match the file name: public class Pulse. Do not leave an older Beat or Dial class name in this file; the compiler expects the class name and the file name to agree.
Inside main, store the counter first:
int k = 1;
That line is not inside the while. It only starts k at 1. Then the loop:
while (k <= 3) {
System.out.println(k);
k = k + 1;
}
The word while is lowercase. Inside the parentheses sits only the test: k <= 3. There is no start and no step inside those parentheses — unlike the for line you saw earlier. The braces hold both the writing line and the step. System.out.println(k); writes the current number with no quotes around k. Then k = k + 1; stores one more under k so the next test can eventually become false. Save the file before you compile.

Program that prints 1 then 2 then 3

This is the version you compile first.

k from 1 to 3

java
public class Pulse {
    public static void main(String[] args) {
        int k = 1;
        while (k <= 3) {
            System.out.println(k);
            k = k + 1;
        }
    }
}

From the folder that holds Pulse.java, run javac Pulse.java. When the parentheses, braces, and semicolons match, javac writes no line of its own. Then run java Pulse — not java Pulse.java. Leave the .java ending off the second command. The same two commands work on Windows and on macOS; this page does not need a different shell spelling for either system. The program should print three lines and stop: 1, then 2, then 3. It should not ask you to type. If the terminal waits for keyboard input, a nextLine call from an older exercise is still in the file. Delete that call and keep the int plus the while above. A wrong screen line does not mean you need a new JDK install.

Walk one full run in order. k starts at 1 before the while. The test k <= 3 is true, so the body runs: println writes 1, then the step stores 2. The test is still true: writes 2, stores 3. The test is still true: writes 3, stores 4. Now k <= 3 is false, so the loop ends. The start ran once, above the loop. The test ran before each pass. The step ran inside the braces after each print. Those three jobs are the same ideas as in for, but they are not packed into one header line. If you omit k = k + 1; and leave k at 1 forever, the test stays true and the same line can print without stopping — that endless case is why the step belongs in the body on this page.

Widen the test

Change only the test so the loop goes one step farther:
int k = 1;
while (k <= 4) {
System.out.println(k);
k = k + 1;
}
Leave the start and the step as they are. Now the screen should show four lines: 1, 2, 3, and 4.

k from 1 to 4

java
public class Pulse {
    public static void main(String[] args) {
        int k = 1;
        while (k <= 4) {
            System.out.println(k);
            k = k + 1;
        }
    }
}

Compile and run again: javac Pulse.java, then java Pulse. You should see four lines. You did not rewrite the println or the step — only the test inside while (…) changed. After you have seen four lines, you can put k <= 3 back to return to the first demo. Both versions are valid Java. They teach the same while shape with a different stopping point.

Read the four-line run once more. k starts at 1. Each true test prints the current k and then adds 1. When k becomes 5, the test fails and the loop stops. The digit 5 never appears on the screen in this demo, because the body only runs while the test is still true.

📊 k and the first demo screen

k before println Screen line
1 1
2 2
3 3

Use the table as a check after the first demo, not instead of a run. Before each println in that demo, k holds the number that will appear on that line. After the third print, the step stores 4 and the test fails. If the screen never stops on a version without a step, put k = k + 1; back inside the braces — do not reinstall Java.

Rules that keep while clear

Parentheses around the test are required. Writing while k <= 3 { … } without parentheses makes javac fail. The body on this page always sits in braces, because two lines belong to each pass: the println and the step. If you leave the step after the closing brace, it runs once after the loop ends — and inside the loop k never changes, so the loop can run forever. Keep the step inside the braces in both demos.

while is not for and not switch. for packs start, test, and step into one header line. while keeps the test alone in its header and asks you to place the start and the step yourself. switch picks among case labels once. Do not leave a Beat for loop or a Dial switch in the same main and wonder why the screen shows a for-style header leftover or the word mid. This session’s main should hold the int and the while only.

A common mix-up is printing the syntax itself: System.out.println("while (k <= 3)");. That writes the characters of the while header. It does not repeat. Another mix-up is putting quotes around k: System.out.println("k"); writes the letter k on every pass. Keep println(k) without quotes when you want the counter on the screen. If the test is already false when the while is first reached — for example k starts at 5 and the test says k <= 3 — the body never runs and the screen stays blank. That is still a valid while; this lesson’s demos keep a test that is true at least once so you can see the printed lines.

What you can do now

You can start a counter above while, put a test in while (…), and keep both the println and the step inside the braces. Changing the test changes how many lines you get. The next page is break and continue, where you leave a loop early or skip one pass without ending the whole loop.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
6
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top