
Loops sometimes have complicated terminating conditions. They do not progress through a simple range of numbers. The while loop construct in the C# language is an ideal choice for this situation. It uses a controlling expression. This expression is evaluated before the loop body statements are executed. It is important to avoid infinite loops.
If you don't know ahead of time exactly how many times you'll want the loop to iterate, use a while loop. McConnell, p. 368
To start, this program shows a while-loop written in the classic way for while-loops. The while keyword is immediately followed by a parenthetical expression.
The expression must evaluate to a boolean result. The expression is evaluated each time the loop is encountered, and if the result of the evaluation is true, the loop body statements are executed. It is also possible to alter the flow of control in the loop using jump statements such as break, return, and goto.
Program that uses while loop with condition [C#]
using System;
class Program
{
static void Main()
{
// Continue in while loop until index is equal to ten.
int i = 0;
while (i < 10)
{
Console.Write("While statement ");
// Write the index to the screen.
Console.WriteLine(i);
// Increment the variable.
i++;
}
}
}
Output
While statement 0
While statement 1
While statement 2
While statement 3
While statement 4
While statement 5
While statement 6
While statement 7
While statement 8
While statement 9Description. This program demonstrates the most popular style to use while-loop constructs in the C# language. The conditional expression in the while loop, which is contained in parentheses, is equivalent to what you might place in an if-statement. This loop will be entered only if the condition i < 10 evaluates to true.
The loop body statements are then executed, and before it executes again, the condition i < 10 is evaluated again and the boolean result is checked. It is important to be careful with the exact behavior of while-loops that contain conditions that prevent execution of the loop body from being taken.

It is also possible, and sometimes useful, to use the true literal in the expression of the while-loop. This program uses a while-loop that has a condition of true. A while-loop with this condition is by definition an infinite loop because the Boolean literal true is always going to evaluate to true.
You can use while(true) loops in the C# programming language to great effect if you carefully provide exit conditions in the loop body statements. The while(true) loop can actually provide a clearer and more self-documenting loop style than other more popular styles.
True ValueProgram that uses true in while loop [C#]
using System;
class Program
{
static void Main()
{
int index = 0;
//
// Continue looping infinitely until internal condition is met.
//
while (true)
{
int value = ++index;
//
// Check to see if limit it hit.
//
if (value > 5)
{
Console.WriteLine("While loop break");
break;
}
//
// You can add another condition.
//
if (value > 100)
{
throw new Exception("Never hit");
}
//
// Write to the screen on each iteration.
//
Console.WriteLine("While loop statement");
}
}
}
Output
While loop statement
While loop statement
While loop statement
While loop statement
While loop statement
While loop break
Readability. The above program would loop infinitely if the conditions in the while loop body were not correct or the variable were not incremented properly. Despite this, there exist studies by academics that show that junior programmers understand the intent of while(true) loops better than some other kinds of loops.
This is probably because they can be read like a spoken sentence and the exit conditions are more clearly documented. You can read more descriptions of studies such as this one in the book Code Complete by Steve McConnell.
Code Complete (Book Review)Summary. The while true loop construct can actually be used to develop a clearer looping layout for your routine. However, you should be aware that this can result in an infinite loop problem, which can be costly in some cases.
You can use a while-loop in the C# language and assign a variable in the parentheses of the while conditional. This allows you to alias variables for use in the loop body. This can be useful if the condition in the while-loop is slow to compute or uses complex expressions. The result is that you can sometimes eliminate duplicate evaluations of the expression and improve performance. Note that there are always ways to rewrite loops written like this in different and equivalent ways.
Program that assigns variable in while condition [C#]
using System;
class Program
{
static void Main()
{
int value = 4;
int i;
// You can assign a variable in the while loop condition statement.
while ((i = value) >= 0)
{
// In the while loop body, both i and value are equal.
Console.WriteLine("While {0} {1}", i, value);
value--;
}
}
}
Output
While 4 4
While 3 3
While 2 2
While 1 1
While 0 0Description. The program demonstrates how you can actually assign a variable in the condition of your while loop. Note that you can assign variables in this way in if-statements and some other constructs as well. However, you cannot declare the type of the variable in the condition; this means you can do a (i = value) but not a (int i = value) in the parentheses.
This technique can lead to some performance improvements in high-level code but can also hinder readability to some extent. The value returned by the evaluation of the assignment is the value assigned to.

The do-while loop is basically an inverted version of the while-loop in that it executes the loop statements unconditionally the first time, and then evaluates the conditional expression specified before executing the statements again. This site has more examples on the do-while loops.
Do While Loop Example
We examined the while-loop statement construct in the C# language and saw how the while-loop can be used to express iterations that can contain many operations. We noted some less-common applications of the while-loop such as the while(true) loop style and how you can assign inside the while-loop condition. Finally, we noted other resources related to while-loops that can improve our understanding of this loop.
Loop Constructs