See how loops iterate and variables change in each iteration
A loop is a control structure that repeats a block of code until a condition is no longer true. Loops help you avoid writing the same code multiple times and process collections of data efficiently.
Repeats code a specific number of times using a counter variable.
`for (int i = 0; i < 5; i++) {
// Code runs 5 times (i = 0, 1, 2, 3, 4)
}
`Repeats code as long as a condition is true.
`while (condition) {
// Code runs while condition is true
}
`Iterates through each item in a collection.
`for (var item in collection) {
// Code runs for each item
}
`1. **Initialization**: Set up the counter variable (i = 0)
2. **Check Condition**: Is the condition true? (i < 5?)
3. **Execute Body**: Run the code inside the loop
4. **Update**: Change the counter (i++)
5. **Repeat**: Go back to step 2
Watch the Memory visualization on the right to see how variables change with each iteration!