Navigation

Related Articles

Back to Latest Articles

C# Basics – Loops – Part 6

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


Andraz Krzisnik
C# Basics – Loops – Part 6

Loops

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.

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.

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.

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)

Comments

Related Articles

C# Tutorial

C# Tutorial: How To Convert a Color Image To Grayscale

Grayscale images are basically black and white images. These images are good for programming, since their pixels hold only intensity values or in other words, shades of gray, they...

Posted on by Andraz Krzisnik
C# Basics

C# Basics – Object Arrays – Part 13

Object Arrays We already mentioned before, that when we create a class we create an object with custom data type. So, as you can see, like strings, integers and other data types,...

Posted on by Andraz Krzisnik