C# Basics – Loops – Part 6

Let's talk about loops and what is their purpose.

Andraz Krzisnik
C# Basics – Loops – Part 6


Let’s say we want to display a message box for each number from 0 to 20. With the knowledge, we have thus far acquired, we can easily achieve this. But what about, if we wanted to display numbers from 0 to 1000? That’s a lot of lines of code.

This is where loops come in handy, because their very purpose is to repeat code held within them, certain amount of times.

While Loop

We use while loops when we don’t know how many times exactly should the loop keep going. So these are perfect in that kind of situation, since we don’t need to define a number how many times it should loop.

While loops check for a condition at the beginning, so as far as that/those condition/s are met, loop will keep repeating the code inside it. If these conditions are broken already at the beginning the loop will stop then.

while (condition)

But if we don’t want our loop ever to break, we can just create a condition that will never be broken, and we get our self an infinite loop. Or instead of creating a condition we can simply write true in place of that condition and we will get  the same effect.

For Loop

We use for loops when we know how many times the loops should repeat. We need to provide three arguments whenever we want to use it. First argument is a variable declaration. Second argument is the highest/lowest number we want the variable to become. Or in other words, it creates a finish line where the loops should stop.

Third and the last argument is an equation that will change value of the variable we declared in the first argument.

After we have everything set, the for loop will, at the beginning, check loop’s variable and its condition,if they check out, it will execute the code within it’s brackets. At the end of each cycle, it will change the loop’s variable with the equation we set in the third argument. When the condition isn’t met anymore, the loop will stop and exit.

for (int i = 0; i < 3; i++)

Note: i++ is the same as i = i + 1

Foreach Loop

When we have an array or a list of items and do something with each item, we can use foreach loops. All we need to provide is the name for a single item and an array that we want to go through.

int[] numberArray = {1, 2, 3, 4};
foreach (int i in numberArray)

Break and Continue

Sometimes, when we run some code within a loop, we need to exit that loop even if the conditions of a loop still hold. If we write break in the middle of the loop, the loop will stop and exit, code that may be written after the break is called, will not be executed in that cycle.

As for continue, it only skips the rest of that loop cycle, and go right to the beginning of the next one.

The only difference between break and continue is when both of them skip the rest of the code in a loop cycle, break will exit the loop all together, while continue will keep the loop running.

Download Examples Next Chapter
Show Comments (0)


Related Articles

C# Tutorial

C# Tutorial: How To Flip an Image Horizontally

Where Do We Start? There are numerous ways you can flip an image horizontally. It’s pretty basic functionality in image processing. I wanted to challenge myself and not just...

Posted on by Andraz Krzisnik
C# Basics

C# Basics – Exception Handling Statements – Part 9

Exception Handling Statements Sometimes, there comes a situation that where our code might crash. We can handle these kind of situations with exception handling statements. By...

Posted on by Andraz Krzisnik