Module 2 · Input, loops and methods: make programs respond · Lesson 6 of 20
Repeat useful work with loops
Could the program print each day’s plan without a separate instruction for every day? Trace one trip around a loop, then find the condition that stops it.
Predict before running
A loop repeats a block of statements. Read this program without running it. Write the four output lines on paper. Then run the complete program in your practice project.
using System;
int day = 1;
int totalMinutes = 0;
while (day <= 3)
{
totalMinutes += 20;
Console.WriteLine($"Day {day}: {totalMinutes}");
day++;
}
Console.WriteLine($"Total: {totalMinutes}");What is the final output line?
Show answer
Correct answer: C
The body runs for day values 1, 2 and 3. Each pass adds 20 minutes.
Option A: This counts only the first pass.
Option B: The condition includes day 3 because it uses <=.
Option D: Day 4 fails the condition, so no fourth addition happens.
Hint: List the day values that satisfy day <= 3.
Trace the changing state
An iteration means one pass through the body. The while condition is checked before every pass. The braces group the statements that belong to that pass. Indentation helps your eyes; the braces decide the group.
| Before the check: day | Is day <= 3? | Total after the body | Next day |
|---|---|---|---|
| 1 | true | 20 | 2 |
| 2 | true | 40 | 3 |
| 3 | true | 60 | 4 |
| 4 | false | Body skipped; total stays 60 | Loop ends |
totalMinutes += 20 means add 20 to the current total and store the result. day++ increases day by one. The total and the counter do different jobs: one collects minutes, the other controls repetition.
Before writing a loop, name its start, its condition and its progress step. Here the start is 1, the condition is day <= 3, and the progress step is day++. If the day never changes, this condition stays true and the program keeps repeating. If you accidentally create such a loop while practising, stop the terminal program with Ctrl+C, then correct the progress step.
Suppose day starts at 4 instead of 1. How many times does this while body run?
Show answer
Correct answer: A
The first condition check is false, so a while loop can run zero times.
Option B: The condition is checked before the first body execution.
Option C: The written limit does not force three repetitions.
Option D: A false initial condition exits immediately.
Hint: Evaluate 4 <= 3 before looking inside the braces.
Put a counter in a for loop
When a counter controls the repetition, a for loop keeps its three parts together: start; condition; progress. This complete replacement prints the same three day numbers, without calculating a total.
using System;
for (int day = 1; day <= 3; day++)
{
Console.WriteLine($"Day {day}");
}Read the header in execution order: initialise day once, check the condition, run the body, increase day, then check again. Do not place an extra semicolon between the header and the opening brace: that would create an empty loop body.
Which header runs the body for day values 1, 2, 3 and 4?
Show answer
Correct answer: B
Starting at 1 and including the upper boundary 4 gives four passes while day increases.
Option A: Using < excludes day 4.
Option C: The initial condition 4 <= 1 is false.
Option D: Decreasing day moves away from the stopping boundary.
Try it independently
Build a four-day plan that adds 25 minutes each day. Predict the total before running. Your final line should be Total: 100. Compare your attempt with this complete solution only after trying.
using System;
int totalMinutes = 0;
for (int day = 1; day <= 4; day++)
{
totalMinutes += 25;
}
Console.WriteLine($"Total: {totalMinutes}");Explain back
Point to the statement that makes each loop finish. Explain why changing <= to < changes the number of passes. Finally, change the plan to two days of 15 minutes and predict 30 before running.
Reference: Microsoft — iteration statements.
Quick reference
- A loop repeats a block.
- while checks its condition before each pass and can run zero times.
- for keeps start, condition and progress in one header.
- Trace the counter and accumulated total separately.
- Check the boundary and the statement that makes the loop stop.
TechInterviewNotes Editorial Team · Updated 27 Oct 2026 ·