Bird
Raised Fist0
Kotlinprogramming~10 mins

For loop with ranges in Kotlin - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - For loop with ranges
Start
↓
Set range start..end
↓
For each value in range
↓
Execute loop body
↓
Next value in range?
No→Exit loop
↩Back to Execute loop body
The loop starts by defining a range, then runs the loop body for each value in that range, moving to the next value until the end.
Execution Sample
Kotlin
for (i in 1..3) {
    println(i)
}
Prints numbers 1, 2, and 3 using a for loop over a range.
Execution Table
IterationiCondition (i in 1..3)ActionOutput
11truePrint i1
22truePrint i2
33truePrint i3
44falseExit loop
💡 Next value would be 4, which is outside the range 1..3, so the loop stops
Variable Tracker
VariableStartAfter 1After 2After 3Final
i-1233
Key Moments - 2 Insights
Why does the loop stop before i becomes 4?
Because the range is from 1 to 3, and 4 is outside this range, so the condition i in 1..3 becomes false before assigning i=4 (see execution_table row 4).
Is the last value in the range included in the loop?
Yes, the range 1..3 includes 3, so the loop runs when i is 3 (see execution_table row 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of i during the second iteration?
A2
B3
C1
D4
💡 Hint
Check the 'i' column in the execution_table at iteration 2.
At which iteration does the loop condition become false?
A3
B4
C1
D2
💡 Hint
Look at the 'Condition' column in the execution_table where it changes to false.
If the range was changed to 1..4, how many times would the loop run?
A3 times
B5 times
C4 times
D2 times
💡 Hint
The loop runs once for each number in the range, including the last number.
Concept Snapshot
for (variable in start..end) {
    // code to repeat
}

- Runs loop from start to end inclusive
- 'variable' takes each value in the range
- Loop stops after end value
- Simple way to repeat code fixed times
Full Transcript
This visual shows how a Kotlin for loop with a range works. The loop starts by setting a range from 1 to 3. Then, for each number in this range, it runs the loop body which prints the current number. The variable i takes values 1, 2, and 3 in order. After i=3, the next value 4 is outside the range, so the loop stops. This example helps beginners see how the loop variable changes and when the loop ends.

Practice

(1/5)
1. What does the following Kotlin code do?
for (i in 1..5) { println(i) }
easy
A. Prints numbers from 1 to 4
B. Prints numbers from 1 to 5 inclusive
C. Prints numbers from 0 to 5
D. Prints numbers from 5 down to 1

Solution

  1. Step 1: Understand the range operator

    The operator 1..5 creates a range including numbers 1, 2, 3, 4, and 5.
  2. Step 2: Loop through the range

    The for loop runs from 1 to 5, printing each number.
  3. Final Answer:

    Prints numbers from 1 to 5 inclusive -> Option B
  4. Quick Check:

    Range 1..5 includes 5 [OK]
Hint: .. means inclusive range from start to end [OK]
Common Mistakes:
  • Thinking .. excludes the last number
  • Confusing range with step or downTo
  • Assuming it counts down automatically
2. Which of the following is the correct syntax for a for loop counting down from 5 to 1 in Kotlin?
easy
A. for (i in 5 downTo 1) { println(i) }
B. for (i in 1..5 step -1) { println(i) }
C. for (i in 5..1) { println(i) }
D. for (i in 1 downTo 5) { println(i) }

Solution

  1. Step 1: Identify correct descending range syntax

    Kotlin uses downTo to count down from a higher number to a lower number.
  2. Step 2: Check each option

    for (i in 5 downTo 1) { println(i) } uses 5 downTo 1, which is correct. for (i in 5..1) { println(i) } uses an invalid range (5..1). for (i in 1..5 step -1) { println(i) } tries to use negative step incorrectly. for (i in 1 downTo 5) { println(i) } reverses the range incorrectly.
  3. Final Answer:

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

    Use downTo for descending loops [OK]
Hint: Use downTo to count down in Kotlin loops [OK]
Common Mistakes:
  • Using .. for descending ranges
  • Trying negative step with .. (not allowed)
  • Swapping start and end in downTo
3. What is the output of this Kotlin code?
var sum = 0
for (i in 1..5 step 2) {
  sum += i
}
println(sum)
medium
A. 6
B. 15
C. 9
D. 10

Solution

  1. Step 1: Understand the loop range and step

    The loop runs from 1 to 5, stepping by 2: values are 1, 3, 5.
  2. Step 2: Calculate the sum of these values

    sum = 0 + 1 + 3 + 5 = 9
  3. Final Answer:

    9 -> Option C
  4. Quick Check:

    1 + 3 + 5 = 9 [OK]
Hint: Add numbers in steps to find sum quickly [OK]
Common Mistakes:
  • Adding all numbers 1 to 5 instead of stepping
  • Using step as a multiplier
  • Confusing step with downTo
4. Find the error in this Kotlin code snippet:
for (i in 10..1 step 2) {
  println(i)
}
medium
A. println cannot be used inside for loop
B. step cannot be used with ranges
C. Variable i is not declared
D. Range 10..1 is invalid for counting down

Solution

  1. Step 1: Analyze the range 10..1

    The range 10..1 tries to count up from 10 to 1, which is invalid because 10 is greater than 1.
  2. Step 2: Understand correct descending syntax

    To count down, use downTo instead of .., e.g., 10 downTo 1 step 2.
  3. Final Answer:

    Range 10..1 is invalid for counting down -> Option D
  4. Quick Check:

    Use downTo for descending ranges [OK]
Hint: .. always counts up; use downTo to count down [OK]
Common Mistakes:
  • Using .. for descending ranges
  • Thinking step fixes descending range
  • Ignoring Kotlin's range direction rules
5. You want to print all even numbers from 2 to 20 inclusive using a for loop in Kotlin. Which code snippet correctly does this?
hard
A. for (i in 2..20 step 2) { println(i) }
B. for (i in 2..19) { if (i % 2 == 0) println(i) }
C. for (i in 2 downTo 20 step 2) { println(i) }
D. for (i in 1..10 step 2) { println(i * 2) }

Solution

  1. Step 1: Identify the range and step for even numbers

    Even numbers from 2 to 20 can be generated by starting at 2 and stepping by 2 up to 20.
  2. Step 2: Check each option

    for (i in 2..20 step 2) { println(i) } uses 2..20 step 2, which prints 2,4,6,...,20 correctly. for (i in 2..19) { if (i % 2 == 0) println(i) } ends at 19 so prints evens up to 18, missing 20. for (i in 2 downTo 20 step 2) { println(i) } counts down incorrectly. for (i in 1..10 step 2) { println(i * 2) } prints 2,6,10,..., not all even numbers.
  3. Final Answer:

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

    Use step 2 from 2 to 20 for evens [OK]
Hint: Use step 2 starting at 2 to print even numbers [OK]
Common Mistakes:
  • Using downTo instead of .. for ascending
  • Forgetting to step by 2
  • Multiplying inside loop incorrectly
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