4.1 Loops

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.

Objectives

  1. Learn what a loop is

  2. Learn the different types of loops

  3. Learn how to write a loop

  4. Learn to identify when to use a loop

File Location

  1. Right click on your solution

  2. Go to Add > New Project and select Console App (.NET Framework)

  3. Name it Loops

Description

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.

While Loops

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:

 class Program
    {
        static void Main(string[] args)
        {
//          1
            int number = 0;
//          2     3
            while(number <= 100)
            {
//              4
                Console.WriteLine(number);
                number = number + 5;
            }

            Console.ReadLine();
        }
    }
  1. Declaring that number is going to be equal to 0

  2. Declaring that this is a while loop

  3. The condition that must be true for the loop to continue to run. "while this is true, continue the loop"

  4. 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.

For Loops

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:

 class Program
    {
        static void Main(string[] args)
        {
//          1
            int number = 100;
//           2      3        4          5
            for(int i = 0; i <= number; i++)
//                6
                Console.WriteLine(i);
            Console.ReadLine();
        }
    }
  1. Declaring that number is equal to 100

  2. Declaring that this is a for loop

  3. Declaring that the for loop will start at 0

  4. Declaring that as long as the index is less than number, continue with the loop

  5. Add one to the index after each loop

  6. 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++.

For Each Loops

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.

 class Program
    {
        static void Main(string[] args)
        {
//          1
            ArrayList nameList = new ArrayList();
            nameList.Add("Paul O");
            nameList.Add("Kenn P");
            nameList.Add("Jenn A");
//          2        3          
            foreach (string name in nameList)
//              4
                Console.WriteLine(name);
            Console.ReadLine();
        }
    }
  1. The array we built and will use a foreach loop to grab data

  2. Declaring that this is a foreach loop

  3. For each string in our array we will call it name

  4. 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.

Challenges:

Bronze

  • Create a for loop that prints the numbers 500 through 1000

Silver

  • Create a for loop that prints the numbers from 0-100 by 5's

Gold

  • 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

Answers

Next: Dictionary Collections

Last updated