Five Minute C# — Lesson 9

Loops and control statements

Alexander Laurence
Five Minute C#
5 min readOct 29, 2018

--

Lessons: 12345678910 ・ 11 ・ 12 ・ 13 ・ 14

Just to reiterate a point

This lesson is about loops! Loops are fun, and incredibly powerful. They allow you to very quickly repeat tasks. There are many types of loops, and we’ll spent this lesson covering them all.

While Loop

Introducing the while loop. It works in a similar way to the if statement, in the sense that it will run the code only if some condition is true. The difference between if and while is that, while will continuously run the code until the condition is false. Let’s take a closer look.

Let’s declare an integer called count.

int count = 0;

Let’s think about a condition we want to be true for our code to repeat itself. As an example, i’ve chosen this condition, count< 10. Remember our conditional operators?

Now we can use a while loop to check if that condition is true.

int count = 0;
while (n < 10)
{
//do something
}

So this will now run the code continuously while count< 10 is true. If for any reason count<10 is false, it will not run the loop. Let’s make it exciting, and let the loop run something for argument’s sake (no pun intended).

We’ll tell count to add 1 to itself (count++), and display it’s value to the console window. You can also use count += 1; as well as count = count +1; whichever way works for you — they’re all the same!

int count = 0;
while (count < 10)
{
count++;
Console.WriteLine(count);
}

This will now run the loop, with count=0, and as long as count< 10 it will add +1 to itself and display its value to the console. count’s value will keep increasing until the while statement is no longer true as it will now be count> 10. This is when the loop stops running. Here’s the output of the code:

0
1
2
3
4
5
6
7
8
9

Remember, loops must have the possibility to end. In our case, it happens when count’s value is 10 or higher, which is powered by count++; being incremented by the loop. If you remove this, then the loop will continue for ever and become an infinite loop — which could crash your program or worse, your computer!

With this in mind, there are a few control statements that can come in useful when writing loops, these are break and continue.

Control Statements

You can manually tell the loop to manually break the operation by using the break control statement.

break;

The other control statement, continue tells the loop to skip to the next iteration.

continue;

We’ll use these in our next loop!

Now that we know about while loops, how about for loops? Luckily, they’re just as easy.

For Loop

Unlike while loops, for loops are used for when you know exactly the amount of times you want to repeat the block of code.

When writing for loops, we have to specify the initialisation and condition. Inside the parenthesis, there are 3 separate sections which are: initializer list, boolean expression, iterator list. In general, it looks something like this:

for (variables count the repeats; conditions; code for each loop)
{
//do something
}

So let’s make it look like real code. We’ll start by declaring a variable again. This will go on the top of the code.

int count;

Next, we will write our for loop. But we need to fill in the parameter.

for(int count;)
{
//do something
}

First, let’s set our variable count at 0.

for(int count = 0;)
{
//do something
}

Now we need the amount of times we want to run the loop. Let’s say 10.

for(int count = 0; count < 10; )
{
//do something
}

And finally, we want count to increment count by 1 for each loop so that the code will loop for a maximum of 10 runs.

for(int count = 0; count < 10; count++)
{
//do something
}

You can prove that it has run 10 times by turning to the console.

for(int count = 0; count < 10; count++)
{
Console.WriteLine("loop number: " + count);
}

Which will give us:

loop number: 0
loop number: 1
loop number: 2
loop number: 3
loop number: 4
loop number: 5
loop number: 6
loop number: 7
loop number: 8
loop number: 9
loop number: 10

There we go. You’ve written your first for loop! Let’s throw in control statements. They will work the same way as with the while loop.

Exercise

Take your time and try and work out what this code does. This will be the hardest challenge so far. The answer is given below.

using System;
class ForLoop
{
public static void Main()
{
for(int i = 0; i < 20; i++)
{
if (i == 10)
{
break;
}
if (i % 2 == 0)
{
continue;
}
Console.Write(i);
}
}
}

Answer

The console will output all the odd numbers from 0 to 10. Let’s have a look why…

Here, the break function tells it to stop once it reaches 10.

if (i == 10)
{
break;
}

And here, it is saying if a number can be divided by 2 and leave no remainder (i.e. if it’s an even number), skip it.

if (i % 2 == 0)
{
continue;
}

Then output the current value of i without writing a new line.

Console.Write(i);

All of this wrapped with a for loop with i having an intial value of 0 until it reaches 20. However, we overided this condition a bit with the break statement.

for(int i = 0; i < 20; i++)
{
if (i == 10)
{
break;
}
if (i % 2 == 0)
{
continue;
}
Console.Write(i);
}

Which gives us the following output:

13579

If you want to add a space between each value for i, then you can change the Console.Write() parameter to this:

Console.Write("{0} ", i);

This just says, format your first value for i (the 0 index) with a space after it. Notice the space after the {0}.

The output should now look like this:

1 3 5 7 9

That’s it for this lesson! We’ll cover more types of loops in our next lesson.

--

--

Alexander Laurence
Five Minute C#

Alex graduated from the University of Edinburgh with a Masters degree in Neuroscience. He spent 3 years teaching and now runs his own tech venture.