Learn
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
while
loops repeat until a condition changesdo...while
loops execute once, and then repeat until a condition changesfor
loops repeat for a specified number of timesforeach
loops repeat for each item in a collection- jump statements, like
break
,continue
, andreturn
are 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.
Instructions
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!
Take this course for free
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.