0
0
Kotlinprogramming~10 mins

Enum entries iteration in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Enum entries iteration
Start
Access enum class
Call entries property
Loop over entries
Process each enum entry
End loop
Finish
The program accesses the enum class, gets all entries, loops over them one by one, and processes each entry.
Execution Sample
Kotlin
enum class Color { RED, GREEN, BLUE }

fun main() {
    for (color in Color.entries) {
        println(color)
    }
}
This code loops over all enum entries of Color and prints each one.
Execution Table
StepActionCurrent EntryOutput
1Start loop over Color.entries
2Process first entryREDRED
3Process second entryGREENGREEN
4Process third entryBLUEBLUE
5End loop - no more entries
💡 All enum entries processed, loop ends.
Variable Tracker
VariableStartAfter 1After 2After 3Final
colorREDGREENBLUE
Key Moments - 3 Insights
Why do we use Color.entries instead of Color.values()?
In Kotlin 1.9+, entries is the recommended property to get enum entries as a list, which is more idiomatic and safer than the older values() method. See execution_table rows 1 and 2 where entries is used.
What happens if the enum has no entries?
The loop will not execute any iterations because entries will be an empty list, so no output is produced. This is shown by the immediate end of the loop in execution_table row 5.
Can we modify enum entries inside the loop?
No, enum entries are constants and immutable. The loop only reads each entry. Attempting to modify them will cause a compile error.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'color' at step 3?
ARED
BGREEN
CBLUE
DNo value
💡 Hint
Check the 'Current Entry' column at step 3 in execution_table.
At which step does the loop finish iterating all enum entries?
AStep 5
BStep 2
CStep 4
DStep 3
💡 Hint
Look for the 'End loop' action in execution_table.
If a new entry YELLOW is added to Color, how many iterations will the loop have?
A5
B3
C4
DCannot tell
💡 Hint
The loop iterates once per enum entry, see variable_tracker showing 3 entries currently.
Concept Snapshot
enum class MyEnum { A, B, C }

for (entry in MyEnum.entries) {
    println(entry)
}

- entries returns all enum entries as a list
- loop iterates over each entry
- entries is preferred over values() in Kotlin 1.9+
Full Transcript
This example shows how to loop over all entries of a Kotlin enum class using the entries property. The program starts by accessing the enum class Color, then calls Color.entries to get a list of all enum entries. It then loops over each entry, assigning it to the variable color, and prints it. The loop runs once for each enum entry: RED, GREEN, and BLUE. After processing all entries, the loop ends. The variable color changes value each iteration to the current enum entry. Beginners often wonder why entries is used instead of values(), or what happens if the enum is empty. The entries property is the modern, idiomatic way to get enum entries as a list. If the enum has no entries, the loop simply does not run. Enum entries are constants and cannot be modified inside the loop. The visual quiz tests understanding of the variable values at each step, when the loop ends, and how adding entries affects iteration count.