9.0: Loops

In this module we'll begin studying loops in C#. NOTE: you will run into some topics that you will not see until further modules.

File Location

  1. Right click on the solution you made in the first module.

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

  3. Name it 09_loops.

Description

Situations will arise where you need to execute a block of code over and over to collect the right data. Maybe we want to check our database for all users who have been active in the last 3 months. Instead of manually checking this, we want to write loops that will look through the database for us. There are multiple types of loops, and it takes a while to learn to use them. Here we start to introduce them to you with some practice.

While Loops

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
        while(number <= 100)
        {
            //3
            Console.WriteLine(number);
            number = number + 5;
            //4
        }
        //5
        Console.ReadLine();
    }
}

While the given number is less than or equal to 100, complete the function.

  1. We declare a variable and initialize its value to 0.

  2. Using the while keyword, we check to see if the number is less than 100. If that is true, we execute the code inside the curly braces.

  3. We print the number. Then, we add 5 to the number. The number will now be valued at 5.

  4. We go back to the top of the loop. Since the number is now 5 and less than 100, it will execute again.

  5. The loop will continue adding 5 to the variable. Once it hits 105, we stop the loop.

For Loops

For loops are probably the most common loop in programming. Let's take a look:

int number = 100;

//1         //2          //3
for(int i = 1; i <= number; i++)
{
    //4
    Console.WriteLine(i);
}
//5

Analysis

There are three expressions in the for loop syntax. 1. The first expression is a variable with initialization. We use for loop syntax and declare a variable called i. This provides an index of where the loop will start. Here we tell the loop to start at 1. 2. The second expression is a conditional expression. We tell the loop that as long as the index is less than or equal to number, which we state is 100, to keep looping. The loop will keep going as long as this expression resolves to true. 3. We increment the variable i by 1 each time we go through the loop. 4. We print the value of i. 5. After i becomes greater than the number variable(101), we leave the loop.

Infinite Looping

We could also decrement here by doing i--, but we would get an infinite loop. i would always be less than 100 so it would loop forever: 1, 0, -1, -2, etc.

Other Loops

There are a couple of other types of loops within C#, but we'll start here. When you're ready, move to the next module to work on some loop challenges.

Last updated