Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use for when an index or explicit counter matters, foreach when you want to process each element of a collection or sequence, and while when a Boolean condition determines whether another pass should begin. The main difference is what controls repetition—not a universal speed ranking.

Quick comparison

Loop What controls repetition Best fit Can the body run zero times?
for Initializer, condition, and iterator grouped in the loop header A counted loop, especially when the index or position matters Yes; the condition is checked before the first pass.
foreach Enumeration of a collection or sequence Processing each supplied element without managing an index Yes; an empty sequence supplies no elements.
while A Boolean condition Repeating while a condition remains true, particularly when the number of passes is not known in advance Yes; the condition is checked before the first pass.

Microsoft recommends foreach for the common case where you read collection elements without using their positions or assigning new values: it communicates that intent and avoids index arithmetic. That is readability guidance, not a claim that foreach is always faster or universally preferable. See Microsoft Learn’s guide to iteration statements.

When to use for

A for loop puts initialization, a Boolean condition, and an iteration step together. Choose it when you need an index, want to visit a range of positions, or need the counter to be visible alongside the stopping condition.

for (int i = 0; i < count; i++)
{
    ProcessAt(i);
}

The execution order matters: the initializer runs once; the condition is checked before each pass; the body runs when that condition is true; and the iterator section runs after the body as control moves toward the next pass. If the condition is false at the start, the body does not run. Microsoft documents this order in the C# iteration statements reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When to use foreach

Use foreach when the task is “do this for each element” and you do not need to manage positions yourself. The iteration variable represents the current element; in the ordinary form described by the language specification, it is read-only within the loop body.

foreach (var item in items)
{
    Process(item);
}

foreach follows the collection or sequence’s enumeration behavior. It is not simply textual shorthand for an indexed for loop: the language specification describes enumeration through an enumerator, including cleanup in its illustrative expanded form. The actual supported enumeration pattern can depend on the collection type. See the C# language specification.

When to use while

Use while when a condition, rather than a fixed counter or one pass per collection element, determines whether another pass should begin. This is useful when the number of repetitions depends on changing state or is not known in advance.

while (ShouldContinue())
{
    DoWork();
}

A standard while checks its condition before entering the body, so it can run zero times. If the condition is initially false, DoWork is not called. Microsoft’s language specification defines this control flow.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How break and continue affect the loops

  • break exits the closest enclosing loop.
  • continue skips the remainder of the current pass and moves control toward the next iteration of the closest enclosing loop.
  • In nested loops, either statement applies to the nearest loop; neither automatically targets an outer loop.

Pay particular attention when translating a for loop into a while loop. The for iterator normally runs as part of reaching the next pass, but a while loop needs its update written explicitly. A continue that bypasses that update can leave the condition unchanged and prevent progress. The C# iteration statements reference describes loop control and iteration behavior.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How to choose

  1. Choose foreach if you need to process each element and do not need to manage its position.
  2. Choose for if the index or counter is part of the task, or if initialization, stopping condition, and update are clearer together.
  3. Choose while if a Boolean condition should decide whether another pass starts and the number of passes is not predetermined.

Prefer the form that makes the unit being processed and the termination condition easiest to see. The cited Microsoft documentation establishes no general performance ranking among these constructs; do not assume one without a benchmark relevant to the specific code and runtime.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.