C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
True: The expression is evaluated each time the loop is encountered. If the result is true, the loop body statements are executed.
Here: This loop will be entered only if the condition i < 10 evaluates to true. A while-loop may never be entered.
Statements: The loop body statements are then executed. Before it executes again, the condition i < 10 is evaluated to true or false.
BoolC# program that uses while-loop with condition
using System;
class Program
{
static void Main()
{
// Continue in while-loop until index is equal to 10.
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 9
Tip: You can use while(true) loops to great effect if you carefully provide exit conditions in the loop body statements.
Style: The while(true) loop is sometimes a clearer and more self-documenting loop style than other more popular styles.
TrueWarning: This program would loop infinitely if the while conditions were not correct or the variable were not incremented properly.
IncrementC# program that uses true in while-loop
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 is 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
Performance: We can sometimes eliminate duplicate evaluations of the expression and improve performance.
C# program that assigns variable in while condition
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 0
C# program that causes convert type error
class Program
{
static void Main()
{
int number = 10;
while (number)
{
}
}
}
Output
Error CS0029
Cannot implicitly convert type 'int' to 'bool'
C# program that avoids error
class Program
{
static void Main()
{
int number = 10;
while (number > 0)
{
number--;
}
}
}
C# program that uses do, while
using System;
class Program
{
static void Main()
{
int number = 0;
// Begin do-while loop.
// ... Terminates when number equals 2.
do
{
Console.WriteLine(number);
// Add one to number.
number++;
} while (number <= 2);
}
}
Output
0
1
2
Note: The C# specification covers the "empty statement" as part of the language's grammar. It is included here for completeness.
Tip: The empty statement can be used anywhere, but it tends to make most sense in a while-loop body.
C# program that uses an empty statement
using System;
class Program
{
static void Main()
{
// Use an empty statement as the body of the while-loop.
while (Method())
;
}
static int _number;
static bool Method()
{
// Write the number, increment, and then return a bool.
Console.WriteLine(_number);
_number++;
return _number < 5;
}
}
Output
0
1
2
3
4
Return: In this program we exit the ReturnIf5 method, and its inner while-loop, when a certain value is reached. We use the return keyword.
ReturnAlso: The break and goto keywords can be used to exit a while-loop. These are all jump statements.
BreakGotoC# program that uses return to exit while-loop
using System;
class Program
{
static void ReturnIf5()
{
// Keep looping until a condition is reached, and return then.
int index = 0;
while (true)
{
if (index == 5)
{
Console.WriteLine("5 REACHED");
return;
}
Console.WriteLine("NOT FOUND");
index++;
}
}
static void Main()
{
ReturnIf5();
}
}
Output
NOT FOUND
NOT FOUND
NOT FOUND
NOT FOUND
NOT FOUND
5 REACHED
Quote: If you don't know ahead of time exactly how many times you'll want the loop to iterate, use a while-loop (Code Complete).
Note: You can read descriptions of studies about programmer understanding in the book Code Complete by Steve McConnell.