BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

C# Programming
.NET 1.1+

C# Program Flow Control: The While Loops

The thirty-first part of the C# Fundamentals tutorial concludes our look at loops and the looping program flow commands available to the C# developer. This article discusses two conditional loop variations, the 'while loop' and 'do-while loop'.

The While Loop

The two looping structures described in the previous parts of the C# Fundamentals tutorial are similar in operation. In most cases, the for loop and the foreach loop will execute for a defined number of iterations controlled directly by the programmer or by the number of items in a collection. In some situations the number of required iterations is not known and cannot be estimated because the loop should continue until a condition is met. This type of program flow can be achieved using the while loop.

The while loop allows you to indicate 'while a condition is true, loop through the following command(s). The syntax for the statement is:

while (condition) command;

The condition element is any predicate. When the value is true, the command provided executes. The command may be a single statement or a code block surrounded by brace characters {}.

The following example uses the while loop to calculate the powers of three between one and one thousand. Unless we have calculated the number of iterations beforehand, we cannot use a for loop to achieve this so a while loop is used instead.

int current = 1;
string output = String.Empty;

// Loop while the value being checked is 1000 or less 
while (current <= 1000)
{
    output += current.ToString() + " ";
    current *= 3;
}

Console.WriteLine(output);      // Outputs "1 3 9 27 81 243 729 "

The while loop's condition is checked each time the looping code is about to be processed. If the condition is false for the first iteration, the body of the loop will never execute. In the following example, the body of the loop is ignored:

int current = 1001;
string output = String.Empty;

// Loop while the value being checked is 1000 or less 
while (current <= 1000)
{
    output += current.ToString() + " ";
    current *= 3;
}

Console.WriteLine(output);      // Outputs ""

The Do-While Loop

The do-while loop allows you to indicate "do this process while a condition is true". The syntax for the statement is similar to the while loop but reverses the positioning of the condition and the command:

do command while (condition);

The principle of the do-while loop is the same as for the while loop. Whilst the condition is true, the command or block of commands is executed. However, because the condition is checked after each iteration of the loop, the code within the loop is guaranteed to run at least once.

The following example shows a do-while loop used to calculate a factorial.

int value = 1;
int factorial = 10;

do
{
    value *= factorial;
    factorial--;
} while (factorial > 1);

Console.WriteLine(value);       // Outputs "3628800"

Further Loop Control

As with the for loop and foreach loop, the break and continue commands are available. The break command stops execution of the loop immediately. The continue command stops execution of the current iteration and restarts the loop if the condition is still met.

1 March 2007