This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 3.2, “How to use Scala
for loops with multiple counters.”
You want to create a loop with multiple counters, such as when iterating over a multi‐dimensional array.
You can create a
for loop with two counters like this: