You set a weekly goal of 75 minutes. Each day you study, and at the end of the week you want one verdict: reached or keep going. This checkpoint combines a loop, which models the repeated work, with a branch, which interprets the result. The question that decides where each line goes is simple: is this something that happens every day, or something that happens once, after the week? Code that answers it wrongly still compiles, and it prints a verdict seven times instead of once.
Predict and run
#include <stdio.h>
int main(void) {
int total = 0;
for (int day = 0; day < 3; day++) {
total += 20;
}
if (total >= 60) {
printf("Goal reached\n");
}
return 0;
}Check your prediction
Goal reachedThe loop ran three times, so total was 60 when the if ran, once, after the loop. Now move the whole if inside the loop body, after total += 20;, and predict again. The message still prints once, but only by luck: it prints on the last day because that is the first day the total reaches 60. Change the goal to 40 and it prints twice. A verdict about the whole week belongs after the loop.
What you will use
- A for loop with a known count. A loop with
dayrunning from 0 while it is less than 5 runs exactly five times. - An accumulator.
totalstarts at 0 before the loop and grows inside it. - A comparison at a boundary.
>=decides whether meeting the goal exactly counts as reaching it. - An if and else after the loop. One verdict, printed once, from the final total.
Plan before you type
- Write down the expected output for 15 minutes a day over 5 days with a goal of 75, and again for a goal of 76.
- Declare
totalandgoalbefore the loop, both initialized. - Write the loop that adds the daily minutes. Print nothing inside it yet.
- After the loop, print the total and run. It should say 75.
- Add the
ifandelsethat print the verdict, and how many minutes are still missing when the goal was not reached. - Run with goals 74, 75 and 76 and check each verdict against your notes.
Prove it can fail
Once both goals give the right verdict, break the comparison on purpose: change >= to >. With a goal of 75 the program should now say Keep going even though you studied exactly 75 minutes. That is why the test value meets the goal exactly: 74 and 76 behave the same with either operator, and only 75 tells them apart. Then move int total = 0; inside the loop body. The build now fails at the final printf with use of undeclared identifier 'total': a variable declared in the body exists only there, and it restarts at 0 every day. Restore both.
Mistakes worth checking for
- The verdict prints several times. The
ifis inside the loop body. Move it after the closing brace of the loop. use of undeclared identifier 'total', or a total of 15 printed inside the loop. The accumulator is declared inside the loop body, so it restarts at 0 every day and is gone after the loop. Declare it before the loop.- The total is 90 or 60. The loop runs six or four times. Check the start value, and whether the condition says "less than" or "at most".
- Exactly 75 says
Keep going. The comparison is>where the brief says "at least", which means>=. - The missing amount is negative. It was printed for a goal that was already reached. Only compute it in the
elsebranch.
Practice
Your turn
Accumulate 15 minutes on each of five days. With goal = 75, print:
Total: 75 of 75
ReachedWith goal = 76, the same program must print:
Total: 75 of 76
Keep going: 1 more minute(s)#include <stdio.h>
int main(void) {
int goal = 75;
// Write your solution here.
return 0;
}Hint 1: the idea
Separate accumulating from reporting. The loop only adds; the lines after the loop only look at the final total.
Hint 2: a first step
Initialize total once, before the loop, and print Total: %d of %d after it. Run it before writing the if.
Show one solution
#include <stdio.h>
int main(void) {
int goal = 75;
int total = 0;
for (int day = 0; day < 5; day++) {
total += 15;
}
printf("Total: %d of %d\n", total, goal);
if (total >= goal) {
printf("Reached\n");
} else {
printf("Keep going: %d more minute(s)\n", goal - total);
}
return 0;
}Total: 75 of 75
ReachedThe loop and the verdict have separate jobs, so each is easy to check on its own. The missing amount is only computed in the else branch, where goal - total is guaranteed to be positive. Tomorrow you will move calculations like this into named functions, so that the same rule can be called from a report and from a test.
Completion is saved in this browser only. It does not measure mastery.