A do–while loop is a control flow statement that executes a block of code at least once, and then repeats the loop as long as a given condition is true.
Click to reveal answer
beginner
How does a do–while loop differ from a while loop?
A do–while loop runs the code block first, then checks the condition. A while loop checks the condition first before running the code block. So, do–while always runs at least once.
Click to reveal answer
beginner
Write the basic syntax of a do–while loop in Java.
do {
// code to run
} while (condition);
Click to reveal answer
intermediate
Why might you choose a do–while loop over a while loop?
You choose a do–while loop when you want the code inside the loop to run at least once, regardless of the condition.
Click to reveal answer
beginner
What happens if the condition in a do–while loop is false on the first check?
The code inside the do block still runs once before the condition is checked. After that, the loop stops because the condition is false.
Click to reveal answer
What is guaranteed when using a do–while loop?
AThe condition is checked before running the loop body
BThe loop body may not run at all
CThe loop runs infinitely
DThe loop body runs at least once
✗ Incorrect
A do–while loop always runs the code block once before checking the condition.
Which part of the do–while loop checks the condition?
AAfter the do block, in the while statement
BInside the do block
CBefore the do block
DThere is no condition in do–while loops
✗ Incorrect
The condition is checked after the do block finishes, in the while statement.
What is the correct way to end a do–while loop in Java?
Awith a semicolon after the while condition
Bwith a comma after the while condition
Cwith no punctuation after the while condition
Dwith a colon after the while condition
✗ Incorrect
A do–while loop ends with a semicolon after the while(condition); statement.
If the condition is false initially, how many times does the do–while loop run?
AZero times
BOnce
CTwice
DIndefinitely
✗ Incorrect
The do block runs once before the condition is checked, so it runs once even if the condition is false.
Which loop is best when you want to run the code at least once?
Awhile loop
Bfor loop
Cdo–while loop
Dif statement
✗ Incorrect
The do–while loop guarantees the code runs at least once.
Explain how a do–while loop works and when you would use it.
Think about the order of execution and the condition check.
You got /3 concepts.
Write a simple Java do–while loop that prints numbers 1 to 3.
Start with int i = 1; then print i and increase it inside the loop.
You got /4 concepts.
Practice
(1/5)
1.
What is the main difference between a do-while loop and a while loop in Java?
easy
A. A do-while loop executes the code block at least once before checking the condition.
B. A while loop executes the code block at least once before checking the condition.
C. A do-while loop never checks the condition.
D. A while loop always executes the code block twice.
Solution
Step 1: Understand do-while loop behavior
A do-while loop runs the code block first, then checks the condition to decide if it should repeat.
Step 2: Compare with while loop
A while loop checks the condition first before running the code block, so it may not run at all if the condition is false initially.
Final Answer:
A do-while loop executes the code block at least once before checking the condition. -> Option A
Quick Check:
do-while runs once before condition check [OK]
Hint: Remember: do-while runs code first, while checks condition first [OK]
Common Mistakes:
Thinking while loop always runs code once
Believing do-while skips condition check
Confusing order of execution and condition check
2.
Which of the following is the correct syntax for a do-while loop in Java?
?
easy
A. do { // code } while (condition);
B. do (condition) { // code };
C. while (condition) { // code } do;
D. do { // code } until (condition);
Solution
Step 1: Recall correct do-while syntax
The correct syntax starts with do, then the code block in braces, followed by while (condition); with a semicolon.
Step 2: Identify the correct option
do { // code } while (condition); matches the correct syntax exactly. Others have wrong order, missing semicolon, or wrong keywords.
Final Answer:
do { // code } while (condition); -> Option A
Quick Check:
Syntax: do { } while (condition); [OK]
Hint: Look for 'do { } while (condition);' with semicolon [OK]
Common Mistakes:
Missing semicolon after while condition
Using 'until' instead of 'while'
Placing condition before code block
3.
What will be the output of the following Java code?
int count = 1;
do {
System.out.print(count + " ");
count++;
} while (count <= 3);
medium
A. 1 2 3 4
B. 2 3 4
C. 1 2 3
D. No output
Solution
Step 1: Trace the loop iterations
Initially, count = 1. The loop prints count, then increments it. It repeats while count <= 3.
Step 2: List printed values
Iterations print: 1 (count=1), 2 (count=2), 3 (count=3). When count becomes 4, condition fails and loop stops.
Final Answer:
1 2 3 -> Option C
Quick Check:
Prints 1 to 3 inclusive [OK]
Hint: Count starts at 1, loop runs while count ≤ 3 [OK]
Common Mistakes:
Thinking loop prints 4 because of increment
Confusing pre-increment and post-increment
Assuming no output if condition false initially
4.
Identify the error in this do-while loop code snippet:
int i = 0;
do {
System.out.println(i);
i++
} while (i < 5);
medium
A. Loop should use while instead of do-while
B. Condition should be inside braces
C. Variable i must be declared inside the loop
D. Missing semicolon after i++
Solution
Step 1: Check syntax inside the loop
The statement i++ is missing a semicolon at the end, which causes a syntax error.
Step 2: Verify other parts
The condition is correctly placed after the loop with a semicolon. Variable declaration outside is valid. Loop type is correct.
Final Answer:
Missing semicolon after i++ -> Option D
Quick Check:
Every statement needs a semicolon [OK]
Hint: Check for missing semicolons inside loop body [OK]
Common Mistakes:
Forgetting semicolon after increment
Misplacing condition inside braces
Thinking variable must be declared inside loop
5.
You want to write a do-while loop that asks the user to enter a positive number and repeats until the user enters a positive number. Which code snippet correctly implements this?
Scanner sc = new Scanner(System.in);
int num;
do {
System.out.print("Enter a positive number: ");
num = sc.nextInt();
} while (???);
hard
A. num > 0
B. num <= 0
C. num == 0
D. num < 0
Solution
Step 1: Understand the loop goal
The loop should repeat while the number is not positive, meaning while it is zero or negative.
Step 2: Choose the correct condition
The condition num <= 0 ensures the loop repeats until the user enters a positive number.
Final Answer:
num <= 0 -> Option B
Quick Check:
Repeat while number is not positive [OK]
Hint: Loop while input is zero or negative to get positive number [OK]
Common Mistakes:
Using condition that stops too early
Checking for positive instead of non-positive
Confusing < and <= operators
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.