0
0
Kotlinprogramming~10 mins

For loop with ranges 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 for loop.

Kotlin
for (i in [1]) {
    println(i)
}
Drag options to blanks, or click blank then click option'
A1 until 5
B1..5
C5..1
D5 until 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'until' which excludes the end number.
Reversing the range like '5..1' which does not work as expected.
2fill in blank
medium

Complete the code to print numbers from 1 up to but not including 5.

Kotlin
for (i in [1]) {
    println(i)
}
Drag options to blanks, or click blank then click option'
A1 until 5
B1..5
C0 until 5
D5 downTo 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using '..' which includes the end number.
Using a reversed range which does not print anything.
3fill in blank
hard

Fix the error in the for loop to count down from 5 to 1.

Kotlin
for (i in [1]) {
    println(i)
}
Drag options to blanks, or click blank then click option'
A5 downTo 1
B5..1
C1 downTo 5
D1..5
Attempts:
3 left
💡 Hint
Common Mistakes
Using '..' with reversed numbers which does not work.
Using 'downTo' with start less than end.
4fill in blank
hard

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

Kotlin
for (i in [1] step [2]) {
    println(i)
}
Drag options to blanks, or click blank then click option'
A2..10
B1..10
C2
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using step 3 which skips numbers incorrectly.
Starting range at 1 which includes odd numbers.
5fill in blank
hard

Fill all three blanks to create a for loop that prints numbers from 10 down to 1, stepping by 2.

Kotlin
for (i in [1] downTo [2] step [3]) {
    println(i)
}
Drag options to blanks, or click blank then click option'
A1
BdownTo
C2
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'until' instead of 'downTo' for counting down.
Using step 1 which does not skip numbers.