Dealing cards in a poker game.
for i from 1 to 5: # 5 cards in a hand
for each player:
deal a card to the player
And then playing the game is also like a for loop. You loop around the table, giving each player an opportunity to make a bet or fold. But this might be better done using a while loop rather than a for loop.
What is a real-world example of a for-loop? - Computer Science Educators Stack Exchange
Example Do While Loop
Even the preface will be confusing to those that don’t understand the title.
More on reddit.comWhat’s a good use of a for loop?
Failure to understand a simple for loop might mean I should stop trying to learn?
Videos
Dealing cards in a poker game.
for i from 1 to 5: # 5 cards in a hand
for each player:
deal a card to the player
And then playing the game is also like a for loop. You loop around the table, giving each player an opportunity to make a bet or fold. But this might be better done using a while loop rather than a for loop.
Reading a book
Reading a book is probably the easiest example.
int numPages = myBook.PageCount();
for (int page = 1; page <= numPages; page++)
{
Literacy.ReadPage(myBook.Pages[page]);
}
This example also naturally displays nested for loops. Each page has a set of lines to iterate over, each line has a set of words, and each word has a set of characters. You can even expand the nesting in the opposite direction - multiple books might be contained in a shelf, multiple shelves might be contained in a library, and multiple libraries might be contained in a library consortium.