A while loop repeats a block of code as long as a given condition is true. It checks the condition before running the code each time.
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. This means it always runs at least once, even if the condition is false at the start.
Click to reveal answer
beginner
Write a simple Kotlin while loop that prints numbers 1 to 3.
var i = 1
while (i <= 3) {
println(i)
i++
}
Click to reveal answer
beginner
Write a Kotlin do-while loop that prints numbers 1 to 3.
var i = 1
do {
println(i)
i++
} while (i <= 3)
Click to reveal answer
intermediate
When should you use a do-while loop instead of a while loop?
Use a do-while loop when you want the code to run at least once, no matter what the condition is. For example, when asking a user for input and you want to check it after the first input.
Click to reveal answer
What happens first in a while loop?
AThe condition is checked
BThe code block runs
CThe loop ends
DThe variable is reset
✗ Incorrect
In a while loop, the condition is checked before running the code block.
How many times does a do-while loop run if the condition is false at the start?
AZero times
BAt least once
CTwice
DInfinite times
✗ Incorrect
A do-while loop always runs the code block at least once before checking the condition.
Which loop is better when you don't know how many times you need to repeat but want to check before running?
Afor loop
Bdo-while loop
Cwhile loop
Dif statement
✗ Incorrect
A while loop checks the condition first, so it is good when you want to run only if the condition is true.
What is the output of this Kotlin code?
var i = 3
while (i < 3) { println(i); i++ }
ANo output
B1 2 3
C3
DError
✗ Incorrect
The condition i < 3 is false at the start, so the while loop does not run and prints nothing.
What keyword is used to repeat a block of code while a condition is true in Kotlin?
Arepeat
Bcycle
Cloop
Dwhile
✗ Incorrect
The keyword 'while' is used to create a while loop in Kotlin.
Explain the difference between a while loop and a do-while loop in Kotlin.
Think about when the condition is checked in each loop.
You got /4 concepts.
Write a Kotlin program using a while loop to print numbers from 1 to 5.
Start with var i = 1 and increase i inside the loop.
You got /4 concepts.
Practice
(1/5)
1. What is the main difference between a while loop and a do-while loop in Kotlin?
easy
A. while loops are faster than do-while loops.
B. while runs code once before checking; do-while checks the condition before running code.
C. while checks the condition before running the code; do-while runs code once before checking.
D. while loops only run once; do-while loops run multiple times.
Solution
Step 1: Understand while loop behavior
A while loop checks its condition before running the code inside it, so if the condition is false initially, the code inside won't run at all.
Step 2: Understand do-while loop behavior
A do-while loop runs the code inside once first, then checks the condition to decide if it should repeat.
Final Answer:
while checks the condition before running the code; do-while runs code once before checking. -> Option C
Quick Check:
Loop condition check timing = while checks the condition before running the code; do-while runs code once before checking. [OK]
Hint: Remember: do-while runs code once before condition check [OK]
Common Mistakes:
Thinking both loops check condition before running code
Confusing which loop runs code first
Assuming do-while runs only once
2. Which of the following is the correct syntax for a do-while loop in Kotlin?
easy
A. do { /* code */ } while (condition)
B. while (condition) { /* code */ } do
C. do (condition) { /* code */ } while
D. while { /* code */ } do (condition)
Solution
Step 1: Recall correct do-while syntax
The do-while loop syntax starts with do, then the code block in braces, followed by while with the condition in parentheses.
Step 2: Compare options to syntax
do { /* code */ } while (condition) matches the correct syntax exactly: do { /* code */ } while (condition). Others have wrong order or misplaced keywords.
Final Answer:
do { /* code */ } while (condition) -> Option A
Quick Check:
Syntax order for do-while = do { /* code */ } while (condition) [OK]
Hint: Remember: do { } while(condition) is the correct order [OK]
Common Mistakes:
Placing while before do
Using parentheses incorrectly
Mixing up braces and parentheses
3. What will be the output of this Kotlin code?
var count = 3
while (count > 0) {
println(count)
count--
}
medium
A. 3 2 1
B. 1 2 3
C. 3 2 1 0
D. 0 1 2 3
Solution
Step 1: Analyze initial value and condition
The variable count starts at 3. The loop runs while count > 0, so it runs for 3, 2, and 1.
Step 2: Trace each iteration and output
Each loop prints the current count then decreases it by 1. So it prints 3, then 2, then 1, then stops before printing 0.
Final Answer:
3 2 1 -> Option A
Quick Check:
Loop prints count down from 3 to 1 = 3 2 1 [OK]
Hint: Count down loops print from start to 1, not zero [OK]
Common Mistakes:
Including 0 in output
Printing in ascending order
Not decrementing count
4. Identify the error in this Kotlin code snippet:
var i = 1
do {
println(i)
i++
} while i <= 3
medium
A. Missing braces around the do block
B. Incorrect use of do keyword
C. Variable i should be declared inside the loop
D. Missing parentheses around the while condition
Solution
Step 1: Check do-while syntax
The while condition must be inside parentheses after the while keyword.
Step 2: Identify the error in the code
The code has while i <= 3 without parentheses. It should be while (i <= 3).
Final Answer:
Missing parentheses around the while condition -> Option D
Quick Check:
Parentheses required for while condition = Missing parentheses around the while condition [OK]
Hint: Always put condition in parentheses after while [OK]
5. You want to write a Kotlin program that asks the user to enter a positive number and keeps asking until the user enters a number greater than zero. Which loop should you use and why?
hard
A. Use a while loop because it checks the condition before asking the user.
B. Use a do-while loop because it asks the user at least once before checking the condition.
C. Use a while loop because it runs the code at least once.
D. Use a do-while loop because it never checks the condition.
Solution
Step 1: Understand the problem requirement
The program must ask the user at least once to enter a number, then repeat if the number is not positive.
Step 2: Choose the correct loop type
A do-while loop runs the code block once before checking the condition, so it ensures the user is asked at least once.
Final Answer:
Use a do-while loop because it asks the user at least once before checking the condition. -> Option B
Quick Check:
Ask user once then check = do-while loop = Use a do-while loop because it asks the user at least once before checking the condition. [OK]
Hint: Use do-while to run code once before condition check [OK]
Common Mistakes:
Choosing while loop which may skip asking once
Thinking do-while never checks condition
Confusing loop behavior with input requirements
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.