for
keywordfor
runs a block code repeatedly, once for each occurrence in a list.
let nums = [1, 2, 3]
for num in nums {
dbg(num)
}
To stop loop evaluation, or to skip a loop iteration, see
break
and
continue
.
As with let
, you can use destructuring in for
loop headers.
let game_scores = [(123, "Alice"), (45, "Bob")]
for (score, name) in game_scores {
dbg(score)
dbg(name)
}
For looping when you don't have a list, see while.