Understanding Loop

Can someone post an example of how a loop can be used in a use case scenario? I’m having a difficult time grasping/visualizing the “loop” control idea.

for (let x of [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) {
    let y = Math.floor(Math.random() * 10);
    switch (true) {
    case y === x: r = 'Bingo!'; break;
    case y < x: r = 'below'; break;
    default: r = 'above'
    }
    console.log(r);
}

This form of loop depends upon iteration. It iterates over the array literal in the of member array. With each iteration, x has new value, and y has a random value in the same range (0…9). We execute the switch in each iteration.

There is also a while loop which does not iterate, but repeats until a condition forces it to terminate. for is supported by an iterator, whereas while is supported by a conditional expression.

do {
    let x = Math.floor(Math.random() * 10);
    let y = Math.floor(Math.random() * 10);
    switch (true) {
    case y === x: r = 'Bingo!'; break;
    case y < x: r = 'below'; break;
    default: r = 'above'
    }
    console.log(r);
} while (r !== 'Bingo!');
 below
 2 above
 2 below
 above
 below
 Bingo!
<- undefined