0
0
Kotlinprogramming~10 mins

Destructuring in collection iteration in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Destructuring in collection iteration
Start with collection
Begin iteration over collection
Destructure each element into parts
Use destructured parts in loop body
Repeat for next element
End when all elements processed
This flow shows how Kotlin loops over a collection, breaking each element into parts using destructuring, then using those parts inside the loop.
Execution Sample
Kotlin
val map = mapOf("a" to 1, "b" to 2)
for ((key, value) in map) {
  println("$key -> $value")
}
This code loops over a map, destructuring each entry into key and value, then prints them.
Execution Table
StepElementDestructured keyDestructured valueActionOutput
1("a", 1)"a"1Print key and valuea -> 1
2("b", 2)"b"2Print key and valueb -> 2
3No more elements--End loop-
💡 All elements processed, loop ends
Variable Tracker
VariableStartAfter 1After 2Final
key-"a""b"-
value-12-
Key Moments - 2 Insights
Why do we write (key, value) in the for loop instead of a single variable?
Because the map entries are pairs, destructuring lets us split each pair into key and value directly, as shown in execution_table rows 1 and 2.
What happens if the collection elements are not pairs?
Destructuring expects the element to have components to split; if not, it causes an error. Here, map entries have two parts, so destructuring works.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'key' at step 2?
A"b"
B"a"
C1
D2
💡 Hint
Check the 'Destructured key' column at step 2 in the execution_table.
At which step does the loop end according to the execution table?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for the row where 'No more elements' is noted in the 'Element' column.
If the map had three entries, how many rows would the execution table have before the loop ends?
A3
B4
C5
D6
💡 Hint
Each entry adds one step plus one final step for loop end, see current table with 2 entries and 3 rows.
Concept Snapshot
Kotlin lets you destructure elements in a for loop.
Use syntax: for ((part1, part2) in collection) { ... }
Works when elements have multiple components (like pairs).
Inside loop, use parts directly.
Loop ends after all elements processed.
Full Transcript
This visual execution shows how Kotlin uses destructuring in collection iteration. We start with a collection, here a map of pairs. The loop goes through each element. Each element is destructured into key and value parts. These parts are used inside the loop body, for example to print them. The execution table shows each step: the element, the destructured key and value, the action, and the output. Variables key and value change each iteration to the current pair's parts. The loop ends when no elements remain. Common confusions include why destructuring is used and what happens if elements are not pairs. The quiz tests understanding of variable values at steps, loop termination, and how the table changes with more elements.