{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Sometimes you just want to repeat yourself. Sometimes you just need to repeat yourself. All programming languages have a concept of loops and C# is no different. \n", "\n", "We have the following keywords available to deliver looping capabilities:\n", "- `for`\n", "- `while`\n", "- `do`\n", "\n", "In this lesson, we'll discuss the different ways to loop over data. This time, we will use the example of dealing cards to players in a game. This is a repeated action that you really wouldn't want to write over and over again:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "var cardsInPlayer1Hand = 0;\n", "var cardsInPlayer2Hand = 0;\n", "\n", "var cardsInDeck = 52;\n", "\n", "cardsInDeck--;\n", "cardsInPlayer1Hand++;\n", "\n", "cardsInDeck--;\n", "cardsInPlayer2Hand++;\n", "\n", "cardsInDeck--;\n", "cardsInPlayer1Hand++;\n", "\n", "cardsInDeck--;\n", "cardsInPlayer2Hand++;\n", "\n", "display($\"Cards left in deck: {cardsInDeck}\");\n", "display($\"Cards in player 1's hand: {cardsInPlayer1Hand}\");\n", "display($\"Cards in player 2's hand: {cardsInPlayer2Hand}\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That's going to be rough to maintain if we have more than 2 players or if we want to deal more than 2 cards to each player. Let's introduce a `for` loop\n", "\n", "## For Loops\n", "[For loops](https://docs.microsoft.com/dotnet/csharp/language-reference/keywords/for?WT.mc_id=visualstudio-twitch-jefritz) are a looping statement that allow you to repeat a block of code depending on a counter expression. The for statement takes the general form:\n", "\n", "`for (Initializer; Condition; Iterator) { CODE TO EXECUTE }`\n", "\n", "The `Initializer` typically initializes a counter variable to be worked with. \n", "\n", "The `Condition` is a test to be executed at the beginning of each attempt to execute the code block. If the `Condition` evaluates to `true` then the code block will be executed.\n", "\n", "The optional `Iterator` code executes after each loop and typically increments the initialized variable , stepping towards the end value.\n", "\n", "Let's write our first `for` loop to deal 5 cards to each of 2 players:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "var cardsInPlayer1Hand = 0;\n", "var cardsInPlayer2Hand = 0;\n", "var cardsInDeck = 52;\n", "\n", "for (var cardsDealt=0; cardsDealt<5; cardsDealt++) {\n", "\n", "\tcardsInDeck--;\n", "\tcardsInPlayer1Hand++;\n", "\n", "\tcardsInDeck--;\n", "\tcardsInPlayer2Hand++;\n", "\n", "}\n", "\n", "display($\"Cards left in deck: {cardsInDeck}\");\n", "display($\"Cards in player 1's hand: {cardsInPlayer1Hand}\");\n", "display($\"Cards in player 2's hand: {cardsInPlayer2Hand}\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That's MUCH easier to read and to work with. Why don't you try these two test scenarios to see if you understand this code:\n", "- Deal 3 cards to each player\n", "- Introduce a variable for the number of cards to deal and set that value to 7\n", "\n", "You can even run `for` loops inside `for` loops. Let's update our sample code to introduce a `for` loop for a different number of players:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "var cardsInPlayer1Hand = 0;\n", "var cardsInPlayer2Hand = 0;\n", "var cardsInPlayer3Hand = 0;\n", "var cardsInPlayer4Hand = 0;\n", "var cardsInDeck = 52;\n", "\n", "for (var cardsDealt=0; cardsDealt<5; cardsDealt++) {\n", "\n", "\tfor (var player=0; player<4; player++) {\n", "\n", "\t\tcardsInDeck--;\n", "\t\tif (player == 0) cardsInPlayer1Hand++;\n", "\t\telse if (player == 1) cardsInPlayer2Hand++;\n", "\t\telse if (player == 2) cardsInPlayer3Hand++;\n", "\t\telse cardsInPlayer4Hand++;\n", "\t\n", "\t}\n", "\n", "}\n", "\n", "display($\"Cards left in deck: {cardsInDeck}\");\n", "display($\"Cards in player 1's hand: {cardsInPlayer1Hand}\");\n", "display($\"Cards in player 2's hand: {cardsInPlayer2Hand}\");\n", "display($\"Cards in player 3's hand: {cardsInPlayer3Hand}\");\n", "display($\"Cards in player 4's hand: {cardsInPlayer4Hand}\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Pretty neat! Notice how we can also use the counter variable `player` to inspect and make decisions about how we walk through the loop and what values we want to update.\n", "\n", "