A for loop repeats a block once for each value. Each iteration can update state.

Program

Play the script to watch value move through the loop and update total.

for_loop.R
values <- c(2, 4, 6)
total <- 0
for (value in values) {
  total <- total + value
}
cat(total, "\n", sep = "")
for `for (value in values)` repeats once per element.
accumulator `total` keeps state across iterations.
iteration Each pass through the loop sees a new `value`.