Exercise 4 of 10 · For loops
Bus Timetable
What you will make
A program that lists five upcoming bus departure times, spaced out by a fixed interval.
The one new idea: Counting for loops (for i := 0; i < n; i++) that compute a new value on each pass
Repeating a calculation a fixed number of times — and getting the loop bound exactly right — is one of the most common patterns in everyday programming, from timetables to batch processing.
Go straight to the code ↓A for i := 0; i < n; i++ loop is Go's standard way to repeat something a known number of times, and getting the middle condition right is the whole trick: i < n runs the body exactly n times (for i equal to 0, 1, ..., n-1), while i <= n sneaks in one extra pass. Inside the loop, you're free to use i in a formula — just remember Go evaluates * before + unless you use parentheses to say otherwise, same as in ordinary math.
Worked example
Here's the same timetable idea with a shorter list and a different interval:
package main
import "fmt"
func main() {
numBuses := 3
intervalMinutes := 15
firstDeparture := 4
fmt.Println("Bus timetable (minutes from now):")
for i := 0; i < numBuses; i++ {
departure := firstDeparture + i*intervalMinutes
fmt.Printf("Bus %d: %d min\n", i+1, departure)
}
}Bus timetable (minutes from now):
Bus 1: 4 min
Bus 2: 19 min
Bus 3: 34 minThree buses print because the loop stops as soon as i reaches 3, and each departure adds one more full 15-minute interval to the first one.
Your turn
The starter program is supposed to print exactly five buses, spaced 12 minutes apart, starting 6 minutes from now — but the loop runs one time too many, and the departure formula groups its terms incorrectly. Fix both marked lines so the timetable matches what the comments describe.
If something goes wrong
If you see six buses instead of five, the loop bound is still off by one. If the numbers grow much faster than expected (or shrink), check the parentheses in the departure formula — multiplying the whole (firstDeparture + i) by the interval is a very different calculation from adding i*intervalMinutes to firstDeparture. Try tracing the first two iterations by hand if the output still looks wrong.
Write your code
Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.
Press Esc then Tab to move keyboard focus out of the code editor.
Output will appear here after you run your code.The runtime is starting in the background. You can type now — it will be ready before you are.
The answer appears here once you have run your code at least once.
Things that often go wrong here
- Using <= instead of < as a loop bound when counting from 0.
- A loop from i := 0 with i <= n runs n+1 times, one more than intended, because it includes both 0 and n.
- Wrapping the wrong terms in parentheses in a mixed +/* expression.
- Go follows normal operator precedence (* before +), so (a + i) * b is a different calculation than a + i*b — parentheses change which operation happens first.
- Printing the loop variable i directly as a 1-based label.
- i starts at 0, so a human-facing label like 'Bus 1' needs i+1, not i itself.
- Re-declaring a loop-body variable with := every iteration by accident and expecting it to persist.
- A variable declared inside a for loop's body is a fresh variable each pass — it doesn't carry its value into the next iteration the way a variable declared before the loop does.
Want a blank editor instead? Open the Go playground.