A loop repeats a block. The two you need now are for, when you know how many times to repeat, and while, when you repeat until something changes.
Counting with for
A for loop has three parts in its parentheses: a starting statement, a condition checked before each pass, and a step that runs after each pass. i++ adds one to i. Trace it on paper first: write the value of i and total after each pass.
int total = 0;
for (int i = 1; i <= 3; i++)
{
total += i * 10;
Console.WriteLine($"Pass {i}: total is {total}");
}
Console.WriteLine($"Finished with {total}");Check your prediction
Pass 1: total is 10
Pass 2: total is 30
Pass 3: total is 60
Finished with 60total += x is short for total = total + x. A variable declared inside the loop's parentheses exists only inside the loop, so i is gone afterwards. total was declared before the loop, which is why it survives.
Most off-by-one mistakes come from the condition. Change <= to < and predict how many passes you get. C# programmers usually count from 0 with <, for reasons you'll see on Day 4 when you meet lists.
Repeating until something changes
A while loop checks its condition before each pass, and it may run zero times. Something inside the loop has to move it toward finishing.
int goal = 100;
int done = 0;
int sessions = 0;
while (done < goal)
{
done += 30;
sessions++;
}
Console.WriteLine($"{sessions} sessions, {done} minutes");Check your prediction
4 sessions, 120 minutesTry a goal of 0. The body never runs, so the answer is 0 sessions. If you ever forget the line that moves the loop along, it will run forever; press Stop to end it. Delete done += 30; and try that now, so you know what it looks like.
Leaving early and skipping
break leaves the loop immediately. continue skips the rest of this pass and moves on to the next one. Both are useful, but each one makes a reader work harder, so reach for them when they make the logic simpler.
for (int minutes = 10; minutes <= 60; minutes += 10)
{
if (minutes == 30)
{
continue;
}
if (minutes == 50)
{
break;
}
Console.WriteLine(minutes);
}Check your prediction
10
20
40Decisions inside a loop
Loops and conditions combine into the most common shape in programming: go through values, decide about each, keep a running count. This one is called the accumulator pattern.
int even = 0;
for (int i = 1; i <= 10; i++)
{
if (i % 2 == 0)
{
even++;
}
}
Console.WriteLine(even);Check your prediction
5Your turn
Weekly growth
Study time starts at 20 minutes on day 1 and grows by 10 minutes each day. For days 1 to 7, print a line such as Day 1: 20 minutes. Afterwards print Total: 350 minutes, and also print how many days were at least 60 minutes, as Long days: 3.
int minutes = 20;
Console.WriteLine($"Day 1: {minutes} minutes");Hint 1: the idea
You know the number of passes, so use a for loop. Keep the total and the count outside it so they survive each pass.
Hint 2: a first step
Print the seven day lines correctly first. Only then add the total, and last of all the count.
Show one solution
int minutes = 20;
int total = 0;
int longDays = 0;
for (int day = 1; day <= 7; day++)
{
Console.WriteLine($"Day {day}: {minutes} minutes");
total += minutes;
if (minutes >= 60)
{
longDays++;
}
minutes += 10;
}
Console.WriteLine($"Total: {total} minutes");
Console.WriteLine($"Long days: {longDays}");Day 1: 20 minutes
Day 2: 30 minutes
Day 3: 40 minutes
Day 4: 50 minutes
Day 5: 60 minutes
Day 6: 70 minutes
Day 7: 80 minutes
Total: 350 minutes
Long days: 3A for loop fits because there are exactly seven days. The order inside the body matters: the day is printed and counted before minutes += 10, so day 1 uses 20 minutes, not 30. The if inside the loop counts long days as it goes, so there's no need for a second pass.
Change >= 60 to > 60 and predict the new count before you run it. Day 5 is exactly on the edge, so this is the test that tells the two rules apart.
Completion is saved in this browser only. It does not measure mastery.