{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# What Are Loops?\n", "Watch the full [C# 101 video](https://www.youtube.com/watch?v=z31m5Up_gSQ&list=PLdo4fOcmZ0oVxKLQCHpiUWun7vlJJvUiN&index=10) for this module." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Loops are a way to repeat an action multiple times. You can use `while` to do that.\n", "> Try out this first loop:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World! The counter is 0\r\n", "Hello World! The counter is 1\r\n", "Hello World! The counter is 2\r\n", "Hello World! The counter is 3\r\n", "Hello World! The counter is 4\r\n", "Hello World! The counter is 5\r\n", "Hello World! The counter is 6\r\n", "Hello World! The counter is 7\r\n", "Hello World! The counter is 8\r\n", "Hello World! The counter is 9\r\n" ] } ], "source": [ "int counter = 0;\n", "while (counter < 10)\n", "{\n", " Console.WriteLine($\"Hello World! The counter is {counter}\");\n", " counter++;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So what happened here? `while` checks if the condition ('counter < 10') is true. If it is, it goes through the loop. Then it checks it again. It will keep going through the loop until the condition is false.\n", "When figuring out branches and loops, it can be helpful to put code into human language. This code is saying: \"Set up counter to equal 0. While the counter is less than 10, print 'Hello World! the counter is {counter}' and then increase the counter by 1\".\n", "\n", "## ++\n", "\n", "`++` is a quick quick way to add one to a variable. You can also do the same with `--`, which subtracts one from the variable.\n", "\n", "## Infinite loops\n", "\n", "It's easy to accidentally make an infinite loop. **Editors note: You can't tell them to create an infinite loop, because the notebook doesn't ever stop itself** If you hit an infinite loop, you have two options: you can always exit out of your program, or you can hit CTRL+C.\n", "\n", "> Challenge: You can't show one on this notebook, because the code doesn't catch itself, but as a challenge, try making an infinite loop in Visual Studio! What are the different ways you might accidentally make it infinite? It can be scary to make a mistake, but always know you can CTRL+C or exit the program." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## do\n", "\n", "You can also make a loop with `do`.\n", "> Try out the following code and see if there's anything different between this and the `while` loop." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World! The counter is 0\r\n", "Hello World! The counter is 1\r\n", "Hello World! The counter is 2\r\n", "Hello World! The counter is 3\r\n", "Hello World! The counter is 4\r\n", "Hello World! The counter is 5\r\n", "Hello World! The counter is 6\r\n", "Hello World! The counter is 7\r\n", "Hello World! The counter is 8\r\n", "Hello World! The counter is 9\r\n" ] } ], "source": [ "int counter = 0;\n", "do\n", "{\n", " Console.WriteLine($\"Hello World! The counter is {counter}\");\n", " counter++;\n", "} while (counter < 10);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Hmm, nothing much seems to different, right?\n", "\n", "> Try changing the conditional to `(counter < 0)` on both codes. What happens?\n", "\n", "In the `while` loop, nothing is printed out, but in the `do` loop, you get one print out. The `do` loop does the action first, then checks the conditional. In contrast, the `while` loop checks the conditional first, then does an action." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## for loop\n", "\n", "`for` is one the most common loops. It will repeat an action for a specific amount of turns. It can look a little intimidating.\n", "> Before you learn all the ins and outs of the code, run the following for loop just to see it working:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World! The counter is 0\r\n", "Hello World! The counter is 1\r\n", "Hello World! The counter is 2\r\n", "Hello World! The counter is 3\r\n", "Hello World! The counter is 4\r\n", "Hello World! The counter is 5\r\n", "Hello World! The counter is 6\r\n", "Hello World! The counter is 7\r\n", "Hello World! The counter is 8\r\n", "Hello World! The counter is 9\r\n" ] } ], "source": [ "for (int counter = 0; counter < 10; counter++)\n", "{\n", " Console.WriteLine($\"Hello World! The counter is {counter}\");\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### What's in the parentheses?\n", "\n", "There are three parts in the `for` loop parentheses: `int counter = 0`, `counter < 10`, and `counter++`.\n", "You can think of them as the start point, the end point, and the step size.\n", "`int counter = 0` is just setting up the counter. You're starting at 0.\n", "`counter < 10` is the conditional that gets checked at the start of every loop. Once the conditional is false (in this case, once counter is not less than 10), the loop is done and the code moves on.\n", "`counter++` is increasing counter by one, taking one step closer to the end. The step is taken at the end of each loop.\n", "> Try messing with the `for` loop set up. How does it change?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Playground\n", "\n", "Play around with what you've learned! Here's some starting ideas:\n", "> Make a loop that counts backwards from 10.\n", ">\n", "> Make a loop that counts forward by 2.\n", ">\n", "> Make a loop that counts from 7 to 22.\n", ">\n", "> Bonus: Make the above with all three loop types." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Playground\r\n" ] } ], "source": [ "Console.WriteLine(\"Playground\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Continue learning\n", "\n", "There are plenty more resources out there to learn!\n", "> [⏩ Next Module - Combining Branches and Loops](https://raw.githubusercontent.com/dotnet/csharp-notebooks/main/csharp-101/09-Combining%20Branches%20and%20Loops.ipynb)\n", ">\n", "> [⏪ Last Module - Branches (if)](https://raw.githubusercontent.com/dotnet/csharp-notebooks/main/csharp-101/07-Branches%20(if).ipynb)\n", ">\n", "> [Watch the video](https://www.youtube.com/watch?v=z31m5Up_gSQ&list=PLdo4fOcmZ0oVxKLQCHpiUWun7vlJJvUiN&index=10)\n", ">\n", "> [Documentation: Branches and Loops in C#](https://docs.microsoft.com/dotnet/csharp/tour-of-csharp/tutorials/branches-and-loops-local?WT.mc_id=Educationalcsharp-c9-scottha)\n", ">\n", "> [Start at the beginning: What is C#?](https://www.youtube.com/watch?v=BM4CHBmAPh4&list=PLdo4fOcmZ0oVxKLQCHpiUWun7vlJJvUiN&index=1)\n", ">\n", "\n", "# Other resources\n", "\n", "Here's some more places to explore:\n", "> [Other 101 Videos](https://dotnet.microsoft.com/learn/videos?WT.mc_id=csharpnotebook-35129-website)\n", ">\n", "> [Microsoft Learn](https://docs.microsoft.com/learn/dotnet/?WT.mc_id=csharpnotebook-35129-website)\n", ">\n", "> [C# Documentation](https://docs.microsoft.com/dotnet/csharp/?WT.mc_id=csharpnotebook-35129-website)" ] } ], "metadata": { "kernelspec": { "display_name": ".NET (C#)", "language": "C#", "name": ".net-csharp" }, "language_info": { "file_extension": ".cs", "mimetype": "text/x-csharp", "name": "C#", "pygments_lexer": "csharp", "version": "8.0" } }, "nbformat": 4, "nbformat_minor": 4 }