This checkpoint combines the three ideas from today in one small program: a loop that visits each day, a condition that decides whether a day counts, and an accumulator that remembers the answer. Each piece is small, and the skill being practiced is fitting them together in the right order. Try it before you open the hints.
What you will use
- A three-part
forloop. The initializer sets the first day, the condition decides when to stop, and the update moves to the next day. The condition must include day 5. - A computed value. Each day's minutes come from a formula,
n * 5, worked out inside the loop. In Go that is writtenday*5whendayis the loop variable. - An
ifwith a boundary. The goal is 20 minutes, and a day that records exactly 20 meets it, so the comparison must include equality. - A counter. It starts at 0 before the loop and goes up by one only when a day meets the goal.
days++is the short form ofdays += 1.
Plan before you type
Describe the program in plain words first, one line per step:
- Start a count of goal days at zero.
- For each day from 1 to 5, work out that day's minutes.
- If the minutes reach the goal, add one to the count.
- After the loop, print the count and nothing else.
Before you run your program, predict the answer by hand. Day 1 records 5 minutes, day 2 records 10, and so on. Write down which days reach 20, count them, and only then compare with your program. A prediction you made first is a real test; a number you accept after seeing it is not.
Print the pieces, then remove them
If your count is wrong, print the day and its minutes inside the loop, for example with fmt.Println(day, day*5). Seeing every pass tells you whether the problem is the range of days, the formula or the comparison. The task asks for only the count, so remove the extra line when you are done.
Mistakes worth checking for
- The count is one too low: the loop stops at day 4 because its condition uses
<instead of<=, or the goal test uses>and skips the day that records exactly 20. - The compiler reports
undefinedfor the counter: it was declared inside the loop, so it doesn't exist after the loop where you print it. (Move the print inside too, and the count restarts on every pass, printing 0 or 1.) Declare it before the loop. - The program prints several numbers: the
Printlnis inside the loop rather than after it. - The compiler reports
declared and not used: a variable for the minutes was declared but theifcompares a different expression.
Your turn
For days 1 through 5, day n records n * 5 minutes. Count how many days meet a goal of 20 minutes and print only the count.
package main
func main() {
// Build your solution here.
}Hint 1: the idea
Update the count only inside the branch that meets the goal.
Hint 2: a first step
Start the counter at zero.
Worked solution
package main
import (
"fmt"
)
func main() {
days := 0
for day := 1; day <= 5; day++ {
if day*5 >= 20 {
days++
}
}
fmt.Println(days)
}2Day 4 records exactly 20 minutes and counts, because the goal uses >=. With >, the count drops to 1.
Before Day 3
Check that you can do each of these without looking back:
- Trace a three-part loop by hand and say exactly when it stops.
- Explain why
else ifbranches are checked in order, and what happens when two conditions are both true. - Say where an accumulator must be declared, and why.
Day 3 gives your values a home of their own: slices hold many values at once, range visits each one, and functions let you name a calculation and reuse it.
Completion is saved in this browser only. It does not measure mastery.