Bird
Raised Fist0
Kotlinprogramming~10 mins

For loop with step and downTo in Kotlin - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - For loop with step and downTo
Initialize i = start
↓
Check: i >= end?
No→EXIT
Yes↓
Execute loop body
↓
Update: i = i - step
↩Back to Check
The loop starts at a number and counts down to an end number, decreasing by a step each time, running the loop body while the condition is true.
Execution Sample
Kotlin
for (i in 10 downTo 2 step 2) {
    println(i)
}
Prints numbers from 10 down to 2, decreasing by 2 each time.
Execution Table
Iterationi valueCondition (i >= 2)ActionOutput
110TruePrint 1010
28TruePrint 88
36TruePrint 66
44TruePrint 44
52TruePrint 22
💡 After printing 2, the next value would be 0 which is less than 2, so the loop stops.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
i108642
Key Moments - 2 Insights
Why does the loop stop when i is 0 even though 0 is less than 2?
The loop condition is i >= 2. When i becomes 0, 0 >= 2 is false, so the loop stops as shown in execution_table after the last printed value.
How does the step 2 affect the values of i?
The step 2 means i decreases by 2 each time (10, 8, 6, ...). This is shown in variable_tracker where i changes by -2 each iteration.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of i at iteration 3?
A8
B6
C4
D2
💡 Hint
Check the 'i value' column at iteration 3 in the execution_table.
At which iteration does the loop condition become false?
A5
B4
C6
D7
💡 Hint
Look at the 'Condition' column in execution_table where it becomes False.
If the step was changed to 3, what would be the value of i at iteration 2?
A7
B6
C5
D8
💡 Hint
With step 3, i decreases by 3 each time: 10, 7, 4... Check variable_tracker logic.
Concept Snapshot
for (i in start downTo end step stepValue) {
  // loop body
}

- Counts down from start to end (inclusive)
- Decreases i by stepValue each time
- Stops when i < end
- Useful for reverse loops with custom steps
Full Transcript
This visual execution shows how a Kotlin for loop with downTo and step works. The loop starts at 10 and counts down to 2, decreasing by 2 each time. Each iteration checks if i is still greater or equal to 2. If yes, it runs the loop body and prints i. Then i decreases by 2. When i becomes less than 2, the condition fails and the loop stops. The variable tracker shows i's value after each iteration. Key moments clarify why the loop stops and how step affects i. The quiz tests understanding of i's values and loop stopping point.

Practice

(1/5)
1. What does the downTo keyword do in a Kotlin for loop?
easy
A. It makes the loop count forwards from a lower number to a higher number.
B. It makes the loop count backwards from a higher number to a lower number.
C. It skips numbers in the loop by a fixed step.
D. It stops the loop immediately.

Solution

  1. Step 1: Understand the purpose of downTo

    The downTo keyword in Kotlin is used to create a range that counts backwards, from a higher number down to a lower number.
  2. Step 2: Compare with other options

    It makes the loop count forwards from a lower number to a higher number. describes counting forwards, It skips numbers in the loop by a fixed step. skipping numbers by step, and It stops the loop immediately. stopping the loop immediately, none of which downTo does.
  3. Final Answer:

    It makes the loop count backwards from a higher number to a lower number. -> Option B
  4. Quick Check:

    downTo = count backwards [OK]
Hint: Remember: downTo means counting downwards [OK]
Common Mistakes:
  • Confusing downTo with counting forwards
  • Thinking downTo skips numbers
  • Assuming downTo stops the loop
2. Which of the following is the correct syntax to loop from 10 down to 1, stepping by 2 in Kotlin?
easy
A. for (i in 10..1 step 2) { println(i) }
B. for (i in 1 downTo 10 step 2) { println(i) }
C. for (i in 10 downTo 1 step 2) { println(i) }
D. for (i in 10 downTo 1 by 2) { println(i) }

Solution

  1. Step 1: Identify correct downTo and step syntax

    The correct syntax to count down with steps is for (i in start downTo end step stepValue). for (i in 10 downTo 1 step 2) { println(i) } matches this.
  2. Step 2: Check other options for errors

    for (i in 1 downTo 10 step 2) { println(i) } has start < end, resulting in empty range. for (i in 10..1 step 2) { println(i) } uses .. which doesn't support down with step correctly. for (i in 10 downTo 1 by 2) { println(i) } uses by instead of step, which is incorrect.
  3. Final Answer:

    for (i in 10 downTo 1 step 2) { println(i) } -> Option C
  4. Quick Check:

    Correct syntax = for (i in 10 downTo 1 step 2) { println(i) } [OK]
Hint: Use downTo with step, not by [OK]
Common Mistakes:
  • Using by instead of step
  • Counting down with .. range
  • Reversing start and end values
3. What is the output of the following Kotlin code?
for (i in 8 downTo 2 step 3) {
    print("$i ")
}
medium
A. 8 5 2
B. 8 6 4 2
C. 8 5 3
D. 8 5 2 1

Solution

  1. Step 1: Determine the loop values

    The loop starts at 8, counts down to 2, stepping by 3. So values are 8, then 8-3=5, then 5-3=2.
  2. Step 2: Confirm the loop stops at 2

    Since 2 is the end value and included, the loop prints 8, 5, and 2.
  3. Final Answer:

    8 5 2 -> Option A
  4. Quick Check:

    8, 5, 2 with step 3 downTo 2 = 8 5 2 [OK]
Hint: Subtract step each time, stop at end value [OK]
Common Mistakes:
  • Including numbers less than end value
  • Using wrong step size
  • Confusing downTo with ..
4. Identify the error in this Kotlin loop:
for (i in 5 downTo 1 step 0) {
    println(i)
}
medium
A. The downTo keyword is misspelled.
B. There is no error; the code runs fine.
C. The loop should use .. instead of downTo.
D. Step value cannot be zero; it causes a runtime error.

Solution

  1. Step 1: Analyze the step value

    The step value is 0, which is invalid and causes a runtime error (IllegalArgumentException).
  2. Step 2: Check other options

    downTo is spelled correctly, and using .. would count upwards, not downwards. So options A, C, and D are incorrect.
  3. Final Answer:

    Step value cannot be zero; it causes a runtime error. -> Option D
  4. Quick Check:

    Step must be positive and non-zero [OK]
Hint: Step cannot be zero; it must be positive [OK]
Common Mistakes:
  • Using zero or negative step incorrectly
  • Confusing downTo with ..
  • Ignoring infinite loop risk
5. You want to print all odd numbers from 15 down to 1 using a Kotlin for loop with downTo and step, without using an if statement. Which code snippet correctly does this?
hard
A. for (i in 15 downTo 1 step 2) { println(i) }
B. for (i in 15 downTo 1 step 1) { if (i % 2 == 1) println(i) }
C. for (i in 15 downTo 1 step 3) { println(i) }
D. for (i in 14 downTo 1 step 2) { println(i) }

Solution

  1. Step 1: Understand the goal

    We want to print odd numbers from 15 down to 1. Starting at 15 (which is odd) and stepping by 2 will print all odd numbers downwards.
  2. Step 2: Evaluate options

    for (i in 15 downTo 1 step 2) { println(i) } starts at 15 and steps by 2 down to 1, printing 15, 13, 11, ..., 1. for (i in 15 downTo 1 step 1) { if (i % 2 == 1) println(i) } uses step 1 with an if filter (disallowed). for (i in 15 downTo 1 step 3) { println(i) } steps by 3, skipping some odds. for (i in 14 downTo 1 step 2) { println(i) } starts at 14 (even), printing evens.
  3. Final Answer:

    for (i in 15 downTo 1 step 2) { println(i) } -> Option A
  4. Quick Check:

    Start odd, step 2 downTo 1 = odd numbers [OK]
Hint: Start at odd number, step 2 downTo 1 for odd numbers [OK]
Common Mistakes:
  • Starting at even number when printing odds
  • Using wrong step size
  • Filtering inside loop instead of stepping
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard