What the exercise asks for
Compute the sum of all whole numbers from 14 to 73, both ends included, and print it. The task has no function around it: it is a handful of statements that declare a variable, run a loop and print the result.
This is the accumulator pattern. You keep a variable that holds the answer so far, and each pass of a loop adds one more piece to it.
The pieces
A running total
Inside a function, sum := 0 declares sum and gives it the type int, inferred from the value 0. The total starts at zero because nothing has been added yet. If it started at any other number, the final answer would be off by that amount.
The three-part for loop
Go has one loop keyword, for. Its classic form has three parts separated by semicolons, with no parentheses:
for i := 14; i <= 73; i++ {
// runs for i = 14, 15, ..., 73
}
i := 14runs once before the loop and declares the counter.i <= 73is checked before every pass. When it is false, the loop ends.i++runs after every pass and adds 1 toi.
The comparison is <=, not <, because 73 is part of the sum. With i < 73 the loop would stop at 72.
Adding inside the loop
sum += i is short for sum = sum + i. Run it for every i, and sum collects 14, then 14 + 15, then 14 + 15 + 16, and so on.
Printing the result
After the closing brace of the loop, fmt.Println(sum) prints the total on its own line. A complete program needs import "fmt" for that call to compile.
The solution in Go
sum := 0
for i := 14; i <= 73; i++ {
sum += i
}
fmt.Println(sum)
The program prints:
2610
You can check the number by hand. There are 73 - 14 + 1 = 60 numbers, and the first and last add up to 14 + 73 = 87. Pairing the numbers from both ends gives 30 pairs that each sum to 87, and 30 * 87 = 2610. The loop agrees with the formula.
Watching the total grow
To see the accumulator at work, print inside the loop on a shorter range, 14 to 17. The pairs show i and the total after that pass:
14 14
15 29
16 45
17 62
Every pass adds the current i to the total from the previous pass. The lesson's version does the same thing for 60 passes and prints only the last total.
Common mistakes in Go
- Printing inside the loop. If
fmt.Println(sum)were inside the braces, it would print 60 running totals instead of one answer. - Using
:=twice for the same name.sum := 0declares the variable. Later changes use=or+=. Writingsum := sum + iinside the loop declares a new variable in the loop's scope and leaves the outer one at 0. - Declaring
suminside the loop. It would be created from scratch on every pass and would not exist after the loop. - Leaving a variable unused. Go refuses to compile a program that declares a variable it never uses. Here
sumandiare both used. - Off-by-one bounds.
i := 14; i < 73gives 2537, which misses 73. Starting at 15 would miss 14. - Putting
++in an expression. In Go,i++is a statement, not an expression, sosum += i++is a syntax error.
Variations
The same shape handles many similar tasks. To add only the even numbers, test i % 2 == 0 inside the loop before adding, or step by two with i += 2 starting from an even value. To count down, start high and use i-- with the condition i >= 14. Only the three loop parts change; the accumulator stays the same.
Key takeaways
- Go's only loop keyword is
for; its three-part form isinit; condition; post. - Use
<=when the last value belongs in the range. - Start an accumulator at the neutral value for the operation: 0 for sums.
- Declare the accumulator before the loop and print it after the loop.