For and while loops
A loop repeats a block while a condition permits it. Use for when initialization, a test and an update naturally belong together. Use while when progress depends on changing state. A do loop checks its condition after running the body, so it always runs at least once.
for (int parcel = 1; parcel <= 3; parcel++){ System.Console.WriteLine(parcel);}The counter starts at one, prints through three and stops before four.
Expected output (reasoned, not executed):
123Trace a for loop
The header for (int i = 1; i <= 3; i++) initializes i once, tests the condition before each iteration, and increments i after the body. The values used by the body are 1, 2 and 3. After the update to 4, the condition is false. An iteration is one execution of the body.
Accumulate and stop
An accumulator stores a running result; initialize it before the loop. A counter or another state change must eventually make the loop condition false. A while loop may run zero times if its first test fails. A do loop uses do { ... } while (condition); and performs at least one iteration.
Avoid off-by-one errors
For a count of n items, starting at zero with i < n yields n iterations when n is nonnegative. Starting at one with i <= n also yields n iterations for ordinary bounded n. Mixing the starting convention and comparison can add or omit an iteration. Test zero, one and a typical count. Use small bounded counts in these examples to avoid overflow or uncontrolled loops.
Examples and walkthroughs
int remaining = 3;int packed = 0;while (remaining > 0){ remaining--; packed++;}System.Console.WriteLine(packed);System.Console.WriteLine(remaining);Each iteration moves one package from remaining to packed. Their sum stays three.
Expected output (reasoned, not executed):
30int n = 0;while (n > 0){ System.Console.WriteLine("while body"); n--;}do{ System.Console.WriteLine("do body");} while (n > 0);The while body never executes. The do body executes once before the same false condition is checked.
Expected output (reasoned, not executed):
do bodyCommon mistake and correction
Do not declare the accumulator inside the loop when it must retain earlier values. For example int total = 0; in the body resets it each iteration. Also ensure every repeating path makes progress toward termination.
Try it yourself
Calculate the sum of integers from 1 through 5 with for and then with while. Repeat with limit 0 and 1. Explain why both loops return zero when the limit is zero.
Next: Foreach, break and continue. These examples target C# 14 / net10.0 and are static study material; compiler and ByteMind execution have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
For and while loops — knowledge check
Apply the lesson concepts to the following scenarios.
How often does the initializer of an ordinary for loop run on entry?
How many iterations for i = 0; i < 4; i++?
What is the minimum number of while iterations?
What is the minimum number of do iterations?
Where should a running total normally be initialized?