Skip to content
Loops
step 1/6

Reading — step 1 of 6

Learn

~3 min readControl Flow

Loops

A loop repeats work so you do not have to copy-paste code. C# has four loop forms, and choosing the right one mostly comes down to one question: do you know how many times you are going around, or are you waiting for something to become true?

for — when you can count the iterations

for (int i = 1; i <= 5; i++) {
    Console.WriteLine(i);        // 1 2 3 4 5
}

The header packs three parts: the initializer (int i = 1, runs once), the condition (i <= 5, checked before every pass — so the body can run zero times), and the increment (i++, runs after each pass). Off-by-one errors live entirely in that condition: i < 5 runs four times, i <= 5 runs five. When a bug involves "one too many" or "one too few", read the condition first.

while and do-while — when you are waiting for a condition

int n = 1;
while (n < 100) {
    n *= 2;                      // doubles until n >= 100
}

while checks the condition first, so the body may run zero times. do { ... } while (cond); checks after, so the body always runs at least once — the natural fit for "prompt, then re-prompt until valid".

foreach — when you have a collection

int[] nums = { 1, 2, 3 };
foreach (int x in nums) {
    Console.WriteLine(x);
}

No index to get wrong; it visits every element in order. Prefer it whenever you do not actually need the index. Flow control works in every loop: break exits the innermost loop immediately, continue skips ahead to the next iteration.

The idiom and the trap: the running product

Accumulating a product — like a factorial — has a fixed shape, and two classic ways to destroy it:

// Idiom: seed with 1, multiply by every i from 1 to n
long result = 1;
for (int i = 1; i <= n; i++) {
    result *= i;
}
// Trap 1: seeding with 0 — anything times zero stays zero
long result = 0;                       // result is 0 forever

// Trap 2: starting i at 0 — the first multiplication is by zero
for (int i = 0; i <= n; i++) result *= i;

Notice something elegant about the idiom: when n is 0, the loop body never runs, so result stays 1 — and 0! is indeed defined as 1. The correct edge-case behavior falls out of the structure for free, with no special-casing.

Why long and not int? Factorials explode: 13! already overflows a 32-bit int. A 64-bit long buys headroom up to 20!.

Your exercise

Read a non-negative integer N and print N! (factorial), computed with a loop — no recursion. The starter seeds long result = 1; and prints it at the end; you write the loop in between.

The grader tests both traps above directly: input 0 expects 1 (the seed must be 1 and the loop must simply not run), and input 5 expects 120 — start i at 1, not 0, because one multiplication by zero turns every answer into 0. Input 10 expects 3628800. Print only the number; the starter's Console.WriteLine(result); already does that.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…