Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › JAVA FUNDAMENTALS
int count = 3;
while (count > 0) {
System.out.println(count);
count--;
}
System.out.println("Done");This program prints 3, 2, 1, and Done, each on its own line. Java reaches the while statement after assigning 3 to count, then evaluates count > 0 before it can run the body. Since 3 > 0 is true, Java enters the body and prints 3. The body contains the statements that perform one countdown step.
After the body finishes, Java returns to the while statement and tests count > 0 again. The body runs only when that test produces true. When the test produces false, Java skips the body and continues with the statement after the loop, which prints Done.
After count is initialized to 3, which action occurs first?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The countdown depends on three connected choices: count starts at 3, the loop continues while count > 0, and count-- changes the value after each printed line. The first two choices describe when the loop may run. The third choice changes the state that the next condition check will inspect.
The while statement does not know that count is supposed to decrease. It does not insert count--, guess which variable to change, or stop after a fixed number of passes. count-- is an ordinary statement written by the programmer. Without a statement that changes the relevant state, the condition may remain true forever.
The first check sees count equal to 3, so the body prints 3 and count-- changes count to 2. The next check sees 2, then the body prints 2 and changes count to 1. A third true check sees 1, prints 1, and changes count to 0.
Java then performs one more condition check. This time 0 > 0 is false, so the body does not execute. Control leaves the loop and prints Done. The program therefore performs four condition checks but only three body executions, and count is 0 before Done is printed.
Type the single statement that changes count from 3 to 2 after the first printed line.
while (count > 0) {
System.out.println(count);
________
}Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
If you remove count--; from the countdown body, count stays 3 after every print. The condition 3 > 0 remains true, so Java prints 3, checks the same true condition, and prints 3 again. It never reaches a false condition and never reaches the statement that prints Done.
int count = 3;
while (count > 0) {
System.out.println(count);
// count-- is missing
}
System.out.println("Done");Changing the condition to count >= 0 has a different consequence. The update still moves count through 3, 2, 1, and 0, but 0 >= 0 is true, so the body also prints 0. After that print, count becomes -1, and -1 >= 0 is false. The loop terminates, but the changed boundary adds an unwanted line instead of preserving the original output.