 .NET 1.1C# 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. However, in some situations the number of required iterations is not known and cannot even be estimated because the loop should continue until a specific condition is met. This type of program flow can be achieved using the while loop.
The while loop allows the programmer to indicate 'while a condition is true, loop through the following command(s). The syntax for the statement reads in much the same way:
while (condition) command;
The condition element of the syntax is any expression that evaluates as a Boolean value. When the value is true, the command provided executes. The command may be a single statement or several lines of code in a code block surrounded by brace characters {}.
The following example uses the while loop to calculate all of 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. This means that if the condition is false for the first iteration, the command within the loop will not execute at all. 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 is a variation of the while loop. It allows the programmer to indicate "do this process while a condition is true". The syntax for the statement is similar to the while loop but similarly 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 the do-while loop used to calculate a specified integer factorial. This algorithm uses a do-while loop so that if the factorial to be calculated is 1!, the loop executes once accordingly.
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.
|