Five Minute C# — Lesson 10
Further loops and implementation
Lessons: 1 ・ 2 ・ 3 ・ 4 ・ 5 ・ 6 ・ 7 ・ 8 ・ 9 ・ 10 ・ 11 ・ 12 ・ 13 ・ 14
Conditions to the left of me, counting variables to the right, here I am, stuck in the middle of a loop
Welcome back to my bad jokes. I see you are hungry for more loops. Let’s get to work!
We now know 2 types of loops. Let’s learn another 2!
For Each Loop
For each is probably one of the easier loops. It iterates the code through a collection of items. Think of it as an extension of the for loop. However, foreach does not use indexes unlike the for loop. This saves us from having to count the number of elements, and iterating through the length of the array.
Let me show you an example of how to write a foreach loop! Start by writing:
foreach()
{
//do something
}
Now we have to specify the parameter. Here we want to specify exactly what we want to collect, this is your datatype (strings, integers, floats, etc) and give it a name. Then specify where you want to collect that from, this can be an array or a list.
Let’s say we want to collect fruits from the array fruitArray:
string[] fruitArray = new string[]
{
"Apples",
"Bananas",
"Pears"
};
Your foreach loop should look like this:
foreach (string fruits in fruitArray)
{
Console.WriteLine(fruits);
}
This applies the same for lists too!
List fruitList = new List<string>()
{
"Apples",
"Bananas",
"Pears"
};foreach (string fruits in fruitList)
{
Console.WriteLine(fruits);
}
Let’s say we want to skip out pears… Let’s use control statements!
List fruitList = new List<string>()
{
"Apples",
"Bananas",
"Pears"
};foreach (string fruits in fruitList)
{
if (fruits == "Pears")
{
continue;
}
Console.WriteLine(fruits);
}
You can even break the loop using the break statement in the same way as we learnt. So there’s no need to write a for loop, use counters, conditions, array lengths, etc… Just use a foreach loop! It’s as simple as that.
Next! The do while loop.
Do While Loop
Remember the while loop? It runs the code based on a boolean value of true or false. So as long the condition is true, the while loop will indefinitely loop the code.
Now, the do while loop is slightly different in the sense that anything specified in the do block of the code will run at least once, while the code within the while block will be dependent on the boolean value of true.
So what we mean is that, even if the while loop is false, it will at least run the do portion of the code at least once. It’s better to demonstrate this than to explain it…
This is the syntax for the do while loop:
do
{
//do something
}
while (boolean expression);
Let’s make it so that it displays (Console.WriteLine()) the value of i (int i), which increases by 1 (i++), while it is less than 10 (i<10). It will look something like this:
int i = 0;do
{
i++;
Console.WriteLine("the value of i: {i}", i);
}
while (i<10);
This will give us the output of:
the value of i: 1
the value of i: 2
the value of i: 3
the value of i: 4
the value of i: 5
the value of i: 6
the value of i: 7
the value of i: 8
the value of i: 9
I hope this makes sense! They’re very easy right?