0
0
Kotlinprogramming~10 mins

For loop with index (withIndex) in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - For loop with index (withIndex)
Start with collection
Get first element and index
Execute loop body with element and index
Get next element and index
More elements?
NoExit loop
Back to Execute loop body
The loop goes through each item in a collection, giving you both the item and its position (index) to use inside the loop.
Execution Sample
Kotlin
val fruits = listOf("Apple", "Banana", "Cherry")
for ((index, fruit) in fruits.withIndex()) {
    println("$index: $fruit")
}
This code loops over a list of fruits, printing each fruit with its index number.
Execution Table
StepindexfruitActionOutput
10AppleStart loop, get first element and indexPrint '0: Apple'
21BananaNext element and indexPrint '1: Banana'
32CherryNext element and indexPrint '2: Cherry'
4--No more elements, exit loopLoop ends
💡 All elements processed, loop ends after index 2
Variable Tracker
VariableStartAfter 1After 2After 3Final
index-012-
fruit-"Apple""Banana""Cherry"-
Key Moments - 3 Insights
Why does the index start at 0 instead of 1?
Indexes in Kotlin collections start at 0 by design, as shown in execution_table rows 1-3 where index values are 0, 1, 2.
What happens when the loop reaches the last element?
After processing the last element (index 2), the loop checks for more elements, finds none, and exits as shown in execution_table row 4.
Can I use the index and element separately inside the loop?
Yes, the loop destructures each pair into index and element variables, so you can use them independently as shown in the code sample and execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'fruit' at step 2?
A"Apple"
B"Banana"
C"Cherry"
Dnull
💡 Hint
Check the 'fruit' column in execution_table row with Step 2
At which step does the loop end according to the execution_table?
AStep 3
BStep 2
CStep 4
DStep 1
💡 Hint
Look at the 'Action' column where it says 'No more elements, exit loop'
If the list had 4 fruits instead of 3, how many steps would the execution_table have before exit?
A5 steps
B3 steps
C4 steps
D6 steps
💡 Hint
Each element adds one step plus one exit step, see current 3 elements with 4 steps total
Concept Snapshot
for ((index, element) in collection.withIndex()) {
    // use index and element
}
- Loops over collection with index starting at 0
- index and element are available inside loop
- Loop ends after last element is processed
Full Transcript
This visual execution shows how a Kotlin for loop with withIndex() works. The loop goes through each item in a list, giving both the item's position (index) and the item itself. The index starts at 0 and increases by 1 each time. The loop runs until all items are processed, then it stops. Variables index and fruit change each step, showing the current position and item. Key moments clarify why index starts at 0, what happens at the last element, and how to use index and element separately. The quiz tests understanding of variable values at steps, loop ending step, and how the number of steps changes with list size.