0
0
Kotlinprogramming~5 mins

While and do-while loops in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AThe condition is checked
BThe code block runs
CThe loop ends
DThe variable is reset
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
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
What is the output of this Kotlin code? var i = 3 while (i < 3) { println(i); i++ }
ANo output
B1 2 3
C3
DError
What keyword is used to repeat a block of code while a condition is true in Kotlin?
Arepeat
Bcycle
Cloop
Dwhile
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.