Well done! In C#, loops are commonly used because they save time, reduce errors, and are easy to read. Being comfortable with each type of loop will make you a better programmer. In review:
- A loop is a structure in programming where the instructions are written once, but a computer can execute them multiple times
- Each execution of those instructions is called an iteration
whileloops repeat until a condition changes
do...whileloops execute once, and then repeat until a condition changes
forloops repeat for a specified number of times
foreachloops repeat for each item in a collection
- jump statements, like
returnare used to add additional control flow to loops
Now that you know a few things about loops, try writing a program that:
- Loops through a piece of text and only prints words that start with the letter “a” to the console to create a poem.
- Loops through a list of numbers and if it is even, print even and if it’s odd, print odd.
- A Choose Your Own Adventure game that uses a while loop to make sure a user chooses a correct option.
Time for some practice! Use the code editor to play around with what you learned in this lesson. If you’re not sure what to do, try one of the extensions above!