Recall & Review
beginner
What is a while loop in Kotlin?
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?
✗ 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?
✗ 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?
✗ 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++ }
✗ 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?
✗ 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.