0
0
Kotlinprogramming~10 mins

For loop with step and downTo 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.

Kotlin
for (i in 1..[1]) {
    println(i)
}
Drag options to blanks, or click blank then click option'
A4
B6
C5
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Using 6 instead of 5 includes an extra number.
Using 4 stops before 5.
2fill in blank
medium

Complete the code to print even numbers from 2 to 10 using step.

Kotlin
for (i in 2..10 step [1]) {
    println(i)
}
Drag options to blanks, or click blank then click option'
A3
B2
C1
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using step 1 prints all numbers.
Using step 3 skips some even numbers.
3fill in blank
hard

Fix the error in the code to print numbers from 5 down to 1.

Kotlin
for (i in 5 [1] 1) {
    println(i)
}
Drag options to blanks, or click blank then click option'
Astep
Buntil
C..
DdownTo
Attempts:
3 left
💡 Hint
Common Mistakes
Using '..' creates an increasing range, causing no output.
Using 'until' is for increasing ranges and excludes the end.
4fill in blank
hard

Fill both blanks to print odd numbers from 9 down to 1.

Kotlin
for (i in 9 [1] 1 [2] 2) {
    println(i)
}
Drag options to blanks, or click blank then click option'
AdownTo
Bstep
C..
Duntil
Attempts:
3 left
💡 Hint
Common Mistakes
Using '..' instead of 'downTo' counts up, not down.
Forgetting step prints all numbers.
5fill in blank
hard

Fill all three blanks to print numbers from 10 down to 2, stepping by 2.

Kotlin
for (i in [1] [2] [3]) {
    println(i)
}
Drag options to blanks, or click blank then click option'
A10
Bstep 2
CdownTo 2
Duntil 2
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'until 2' counts up and excludes 2.
Placing step before downTo causes syntax errors.