C# While Loop: Syntax, Behavior, and Pitfalls
Learn the C# while loop syntax, how condition evaluation works, and common mistakes to avoid when using while and do-while loops.

The C# while loop is a pre-test loop that repeatedly executes a block of code as long as a boolean condition evaluates to true. It's one of the most straightforward iteration constructs in the language, but its simplicity hides several behaviors that can lead to subtle bugs if you're not careful.
Basic Syntax and Behavior
The while loop has a minimal structure:
while (condition)
{
// body
}The condition is evaluated before each iteration. If it returns true, the body executes; if it returns false, control passes to the first statement after the loop. This means the body may never execute if the condition is initially false.
Here's a simple example that prints numbers from 0 to 4:
int i = 0;
while (i < 5)
{
Console.WriteLine(i);
i++;
}Each iteration checks whether i is still less than 5. The increment inside the body ensures the loop eventually terminates. Without that increment, the condition would remain true forever, creating an infinite loop.
How the Condition Is Evaluated
The condition must be an expression that evaluates to a bool. C# does not implicitly convert integers or other types to boolean, so you cannot write while (1) as you might in C or JavaScript. Instead, you need an explicit comparison:
int value = 1;
while (value != 0)
{
// ...
}The condition is re-evaluated at the start of every iteration. If the condition depends on variables modified in the body, the loop may behave differently than expected if those changes affect the condition. This is the core mechanism that makes while loops flexible but also prone to off-by-one errors.
The do-while Variant
C# also provides the do-while loop, which is a post-test loop. The body executes at least once before the condition is checked:
do
{
Console.WriteLine("At least once");
} while (condition);This is useful when you need to perform an action before validating a condition, such as prompting for user input and re-prompting until the input is valid. The syntax requires a semicolon after the while clause.
The key difference is the number of guaranteed executions: a standard while loop may run zero times, while a do-while loop always runs at least once. Choose based on whether the first iteration should happen unconditionally.
Common Pitfalls and How to Avoid Them
Infinite Loops
The most common mistake is forgetting to update the variable that controls the condition. For example:
int count = 0;
while (count < 10)
{
Console.WriteLine(count);
// Missing count++
}This will loop forever. Always ensure that the condition can become false through some path in the body, or use break to exit explicitly.
Off-by-One Errors
Because the condition is checked before execution, the loop runs while the condition is true. If you want to iterate exactly n times, start the counter at 0 and use < n. Starting at 1 and using <= n also works but is less idiomatic and can lead to confusion when indexing arrays.
Modifying the Loop Variable Inside the Body
Changing the loop variable in unexpected ways can make the loop skip iterations or terminate early. For example:
int i = 0;
while (i < 10)
{
if (someCondition)
{
i += 2; // skips an iteration
}
i++;
}This is not inherently wrong, but it makes the control flow harder to follow. If you need conditional advancement, consider using continue or restructuring the logic.
Using break and continue
The break statement exits the loop immediately, regardless of the condition. It is often used to stop processing when a specific value is found:
int[] numbers = { 1, 2, 3, 4, 5 };
int i = 0;
while (i < numbers.Length)
{
if (numbers[i] == 3)
{
break;
}
i++;
}The continue statement skips the rest of the current iteration and jumps to the next condition check. This is useful for filtering out values without breaking the loop:
int i = 0;
while (i < 10)
{
i++;
if (i % 2 == 0)
{
continue;
}
Console.WriteLine(i); // prints odd numbers
}Both statements work in do-while loops as well. Be careful with continue in a while loop: if the increment is placed after the continue statement, it will be skipped, potentially causing an infinite loop. In the example above, the increment is placed before the continue, so it's safe.
Performance: while vs. for
In ordinary C# code, while and for are equivalent in expressive power, and there is no meaningful performance advantage to choosing one over the other. The important difference is readability and intent.
Use a while loop when the number of iterations is not known in advance and depends on a condition that may change during execution. Examples include reading lines from a file until EOF, polling a resource until it becomes available, or processing user input until a sentinel value is entered.
Use a for loop when you are iterating over a fixed range or an indexable collection, because it keeps the loop control logic together and reduces the chance of forgetting to update the counter.
Avoid expensive method calls in the condition if the loop runs many times; the condition is evaluated before every iteration.
Real-World Example: Reading User Input
A common pattern is to read input until the user enters a specific value. The while loop is ideal because the number of iterations is unknown:
string input;
while ((input = Console.ReadLine()) != null && input != "quit")
{
Console.WriteLine($"You entered: {input}");
}This loop reads a line, assigns it to input, and checks that input is not null and not equal to "quit". Checking for null also covers end-of-file, where Console.ReadLine() returns null. The assignment inside the condition is valid in C# and works because the assignment expression returns the assigned value. This pattern is concise but can be less readable; an alternative is to use a do-while loop if you want to prompt at least once.
Another example is processing a queue of work items:
Queue<int> tasks = new Queue<int>();
while (tasks.Count > 0)
{
int task = tasks.Dequeue();
Process(task);
}Here, the condition depends on the queue's count, which changes as items are dequeued. This is a natural fit for a while loop because the termination condition is dynamic.
Compatibility Notes
The syntax and semantics described in this article apply to current C# and .NET versions. The condition must always be a bool; you cannot use a bool? directly. Use .HasValue, flag == true, or flag ?? false to convert it to a non-nullable bool.
Inside an async method, you can await in the body of a while loop; the loop control flow remains the same.
Choosing Between while and for in Practice
The decision between while and for is largely about code clarity. If the loop is controlled by a counter that increments uniformly, a for loop is more readable because it keeps the initialization, condition, and increment together. If the loop is controlled by an external condition that changes inside the body, a while loop expresses that intent more directly.
Consider this for loop:
for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);
}The equivalent while loop requires the counter to be declared outside and incremented manually. The for version is less error-prone because the increment is part of the loop header, making it harder to forget. On the other hand, a while loop that reads from a stream until the end is clearer than trying to force that into a for loop with a counter.
There is no rule that says you must use one over the other. Choose the construct that makes the control flow obvious to the next developer who reads the code. If the loop's condition is not a simple counter, a while loop is usually the better choice.
A final note: avoid using while loops to iterate over collections when a foreach loop is more appropriate. foreach handles the enumerator and throws an exception if you modify the collection during iteration, which is safer than manually indexing. Use while when you need index-based access or when you are not iterating over a collection at all.
Try matching browser tools
All toolsSmall utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related C# guides
All C# guidesC# Dispose vs Destructor: Key Differences Explained
c# dispose vs destructor: Understand the difference between Dispose and destructors in C#, when each runs, and how to manage unmanaged resources correctly.
C# Variable Declaration: Syntax and Practical Usage
Learn how to declare variables in C# with explicit types, var, and nullable annotations, and understand how defaults, scope, and readability affect your choices.
C# Primary Constructors for Classes: Syntax and Usage
Learn how to use C# primary constructors for classes: syntax, initialization, dependency injection, and limitations with practical examples.
C# IComparer: Custom Sorting with IComparer<T>
Learn how to implement IComparer<T> in C# to define custom sort order for arrays and lists, handle nulls, and avoid common performance pitfalls.
More in C#
All C# guidesThe guides filed next to this one in C#, in library order.