Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › JAVA FUNDAMENTALS
A do-while loop enters its body immediately, then evaluates its condition. That order differs from a while loop, which checks its condition before entering the body. In this PIN prompt, pin starts with the accepted value 4321, but the program still asks for a PIN because execution enters the do body without first testing pin != 4321.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int pin = 4321;
do {
System.out.print("Enter PIN: ");
pin = input.nextInt();
} while (pin != 4321);
System.out.println("Accepted");
}
}The initialization gives pin the value 4321 before the loop starts. That value does not prevent the body from running, because the condition belongs at the end of the do-while statement. The body prompts for input and stores the input in pin before Java evaluates pin != 4321 for the first time.
When pin starts as 4321, what happens first in this program?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Each visit to the do body completes one prompt and read. After the first input, 1111, the condition pin != 4321 is true, so control returns to the body. After the second input, 2468, the condition is true again, so the prompt runs again. After the third input, 4321, the condition is false, so the loop ends and execution reaches the output statement.
| BODY EXECUTION | INPUT STORED IN PIN | PIN != 4321 | NEXT ACTION |
|---|---|---|---|
| 1 | 1111 | true | Return to the body |
| 2 | 2468 | true | Return to the body |
| 3 | 4321 | false | Print "Accepted" |
The condition does not describe whether the current input was accepted. It decides whether another body execution is needed. A true result sends control back to the prompt, while a false result lets the statement after the loop run. With these inputs, the body executes three times and the condition is checked three times.
The closing line has a required form: } while (pin != 4321); The closing brace ends the do body, while introduces the condition, and the final semicolon completes the entire do-while statement. The semicolon is not optional punctuation added after the loop.
do {
System.out.print("Enter PIN: ");
pin = input.nextInt();
} while (pin != 4321);If you remove the final semicolon, Java reports a compilation error because the do-while statement is incomplete. If you place a semicolon after do, the empty statement becomes the do body and the following block is no longer the intended body of the loop. That changes the program's structure instead of completing the PIN prompt.
Restore the exact missing token after } while (pin != 4321) in the PIN prompt.
} while (pin != 4321)___Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
A PIN prompt must read an attempt before it can decide whether another attempt is needed. The do-while structure expresses that order directly: enter the body, read the PIN, evaluate the condition, and either repeat or continue to Accepted.
int pin = 4321;
while (pin != 4321) {
System.out.print("Enter PIN: ");
pin = input.nextInt();
}
System.out.println("Accepted");With the same initial pin of 4321, a while form checks pin != 4321 before its body. That comparison is false immediately, so it skips the prompt and reaches Accepted without reading 1111, 2468, or 4321. The do-while form is the correct shape when the first action must happen before the repetition test.