0
0
Kotlinprogramming~10 mins

While and do-while loops in Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print numbers from 1 to 5 using a while loop.

Kotlin
var i = 1
while (i [1] 5) {
    println(i)
    i++
}
Drag options to blanks, or click blank then click option'
A<=
B<
C>
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'i < 5' will stop before printing 5.
Using 'i > 5' will never enter the loop.
2fill in blank
medium

Complete the code to print numbers from 1 to 5 using a do-while loop.

Kotlin
var i = 1
do {
    println(i)
    i++
} while (i [1] 6)
Drag options to blanks, or click blank then click option'
A<
B>
C<=
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' will print 6 as well.
Using '>' will cause the loop to run only once or not at all.
3fill in blank
hard

Fix the error in the while loop condition to avoid an infinite loop.

Kotlin
var count = 0
while (count [1] 5) {
    println(count)
    // Missing increment
}
Drag options to blanks, or click blank then click option'
A<=
B>=
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>=' or '>' will cause the loop to never run or run infinitely.
Not incrementing the counter causes infinite loop.
4fill in blank
hard

Fill both blanks to create a while loop that prints even numbers from 2 to 10.

Kotlin
var num = 2
while (num [1] 10) {
    println(num)
    num [2] 2
}
Drag options to blanks, or click blank then click option'
A<=
B+=
C-=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' will exclude 10.
Using '-=' will decrease num and cause infinite loop.
5fill in blank
hard

Fill all three blanks to create a do-while loop that prints numbers from 10 down to 1.

Kotlin
var i = [1]
do {
    println(i)
    i [2] 1
} while (i [3] 0)
Drag options to blanks, or click blank then click option'
A10
B-=
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Starting at 1 will print only one number.
Using '+=' will increase i and cause infinite loop.
Using '<' in condition will cause infinite loop.