4.1 Loops
Last updated
Last updated
Lets look at loops before going on to more collection types.
Welcome to your loops Module. In this module we'll be writing loops, learning about loops, and hopefully showing you how to make your work easier.
Learn what a loop is
Learn the different types of loops
Learn how to write a loop
Learn to identify when to use a loop
Right click on your solution
Go to Add > New Project and select Console App (.NET Framework)
Name it Loops
Situations will arise where you need to execute a block of code over and over to collect the right data. These are referred to as loops. Usually, these functions are executed sequentially - first, second, third, and so on.. However, there are various controls you can provide to execute a different path.
Diagram illustrating the basic flow of a loop:
While loops might be the simplest, so we will start with that. While loops execute a block of code as long as the condition you give it is true. Once the condition is false, it will stop. Below is an example:
Declaring that number
is going to be equal to 0
Declaring that this is a while
loop
The condition that must be true for the loop to continue to run. "while this is true, continue the loop"
Indicating what will happen each loop. We will print number
to the console and add 5 to the index
This loop is saying
While the given number is less than or equal to 100, complete the function.
We start the loop at 0 and tell it to add 5 each time it goes through the loop. Once it hits 100, we stop the loop.
With a for loop, you explicitly tell the function how many times you want it to run. You use this when you know the exact amount of iterations. Here is an example:
Declaring that number is equal to 100
Declaring that this is a for
loop
Declaring that the for loop will start at 0
Declaring that as long as the index is less than number
, continue with the loop
Add one to the index after each loop
Print each number to the console
Here we use the for loop syntax. Inside the parentheses, we tell the function where the index will start, or where the loop will start. We tell the loop to start at 1. Then we tell it that as long as the index is less than or equal to number
, which we state is 100, go to the next number in the index i++
.
foreach
loops operate on collections of items, for instance arrays or other built-in list types. In this example we will make an ArrayList called nameList
and use a foreach loop to grab each name from the list. Try it out.
The array we built and will use a foreach loop to grab data
Declaring that this is a foreach loop
For each string in our array we will call it name
After each instance of the loop it will print name
There are a couple other types of loops within C# but these are good ones to start with.
Create a for loop that prints the numbers 500 through 1000
Create a for loop that prints the numbers from 0-100 by 5's
Create a for loop that prints the numbers 1-100 to the console. If the # is divisible by 3, print Fizz
instead of the number, if it's divisible by 5, print Buzz
, if it's divisible by both 3 and 5, print FizzBuzz
Next: Dictionary Collections
Note: you will need to bring in a using statement for ArrayList()
to work 1. Hover over ArrayList()
2. Press the CTRL
key and the .
key 3. Press Enter
and it will bring in System.Collections