Summary: in this tutorial, you’ll learn how to use the C# while
statement to execute a block while a boolean expression is true
.
Introduction to the C# while statement
The while
statement evaluates a boolean expression and executes a block repeatedly as long as the expression is true
. Here’s the syntax of the while
statement:
while (expression)
{
// statement
}
Code language: C# (cs)
How it works.
The expression
, which follows the while
keyword, must be a boolean expression that evaluates to true
or false
.
The while
statement evaluates the expression
first. If the expression
evaluates to true
, it’ll execute the block inside the curly braces.
Once completed executing the block, the while
statement checks the expression
again. And it’ll execute the block again as long as the expression
is true
.
If the expression
is false
, the while
statement exits and passes the control to the statement after it.
Therefore, you need to change some variables inside the block to make the expression
false
at some point. Otherwise, you’ll have an indefinite loop.
Since the expression
is checked at the beginning of each iteration, the while
statement is often called a pretest loop.
The following flowchart illustrates how the C# while statement works.
C# while statement examples
Let’s take some examples of using the while
statement.
1) Simple C# while statement example
The following example uses the while
loop statement to output five numbers from 1 to 5 to the console:
int counter = 0;
while(counter < 5)
{
counter++;
Console.WriteLine(counter);
}
Code language: C# (cs)
Output:
1
2
3
4
5
Code language: C# (cs)
How it works.
First, declare a counter
variable and initialize it to zero.
Second, enter the while
loop because the following expression is true
:
counter < 5
Code language: C# (cs)
Third, increase the counter
by one and print it out to the console; repeat this step as long as the counter
is less than 5.
2) Using the C# while statement to calculate the average
The following program prompts users to enter a list of numbers and calculate the average:
double number = 0,
total = 0,
count = 0,
average = 0;
string input = "";
Console.WriteLine("Enter a list of numbers to calculate the average (Q - quit):");
while (input != "Q" && input != "q")
{
input = Console.ReadLine();
if (input != "Q" && input != "q")
{
number = Convert.ToDouble(input);
total += number;
count++;
}
}
if (count > 0)
{
average = total / count;
}
Console.WriteLine($"Average:{average}");
Code language: C# (cs)
How it works.
First, declare variables and initialize them:
double number = 0,
total = 0,
count = 0,
average = 0;
string input = "";
Code language: C# (cs)
Second, print out the instruction:
Console.WriteLine("Enter a list of numbers to calculate the average (Q - quit):");
Code language: C# (cs)
Third, prompt users to enter a number until they enter the letter Q
or q
. In each iteration, calculate the total and count the entered numbers:
while (input != "Q" && input != "q")
{
input = Console.ReadLine();
if (input != "Q" && input != "q")
{
number = Convert.ToDouble(input);
total += number;
count++;
}
}
Code language: C# (cs)
Finally, calculate the average if users enter at least one number and output it to the console:
if (count > 0)
{
average = total / count;
}
Console.WriteLine($"Average:{average}");
Code language: C# (cs)
In the following output, we enter three numbers 10, 20, and 30. And the program shows the average as 20:
Enter a list of numbers to calculate the average (Q - quit):
10
20
30
q
Average:20
Code language: C# (cs)
Summary
- Use the
while
statement to execute a block as long as a boolean expression istrue
.