The while, do, for and foreach loop

The while, do, for and foreach loop

While

It is a control flow statement for repeated execution of code when a given condition evaluates to true.

The syntax is as

while (expression)
{
statement(s);
}

While execute all of the statements enclosed by in the while block as specified by the curly brackets. All of the statements are repeatedly executed till the expression evaluates to true. As for example to find the sum of values from a range of numbers is given below

using System;

public class Whileloop
{
static void Main()
{
int i = 0, sum = 0;

while (i < 10)
{
i++;
sum = sum + i;
}
Console.WriteLine(sum);
}
}

As in the example above, while consists of three main parts of Initialization, test and update. Each execution of the statement is called a cycle.

int i = 0;

The i variable is initiated and used as a counter.

while (i < 10)
{

}

The expression inside the square brackets following the while keyword is the second phase, the testing. The statements in the body are executed, until the expression is evaluated to false.

i++;

The last, third phase of the while loop. The updating. We increment the counter. Note that improper handling of the while loops may lead to endless cycles.

It is possible to run the statement at least once. Even if the condition is not met. For this, we can use the do while keywords.

using System;

public class CSharpApp
{
static void Main()
{
int count = 0;

do {
Console.WriteLine(count);
} while (count != 0);
}
}

First the iteration is executed and then the truth expression is evaluated.

For

When the number of cycles is know before the loop is initiated, we can use the for statement. In this construct we declare a counter variable, which is automatically increased or decreased in value during each repetition of the loop.

using System;

public class CSharpApp
{
static void Main()
{
for (int i=0; i<9; i++)
{
Console.WriteLine(i);
}
}
}

In this example, we print numbers 0..9 to the console.

for (int i=0; i<9; i++)
{
Console.WriteLine(i);
}

There are three phases. First, we initiate the counter i to zero. This phase is done only once. Next comes the condition. If the condition is met, the statement inside the for block is executed. Then comes the third phase; the couter is increased. Now we repeat the 2, 3 phases until the condition is not met and the for loop is left. In our case, when the counter i is equal to 9, the for loop stops executing.

foreach

The foreach construct simplifies traversing over collections of data. It has no explicit counter. The foreach statement goes through the array or collection one by one and the current value is copied to a variable defined in the construct.

using System;

public class CSharpApp
{
static void Main()
{
string[] planets = { “Mercury”, “Venus”,
“Earth”, “Mars”, “Jupiter”, “Saturn”,
“Uranus”, “Neptune” };

foreach (string planet in planets)
{
Console.WriteLine(planet);
}
}
}

In this example, we use the foreach statement to go through an array of planets.

foreach (string planet in planets)
{
Console.WriteLine(planet);
}

The usage of the foreach statement is straightforward. The planets is the array, that we iterate through. The planet is the temporary variable, that has the current value from the array. The foreach statement goes through all the planets and prints them to the console.

$ ./planets.exe
Mercury
Venus
Earth
Mars
Jupiter
Saturn
Uranus
Neptune

Running the above C# program gives this output.

The if and switch statement
Python Programming Tutorial | The break and continue statement

Get industry recognized certification – Contact us

keyboard_arrow_up
Open chat
Need help?
Hello 👋
Can we help you?