What the exercise asks for
The task is to compute the sum of all whole numbers from 14 to 73, both ends included, using a for loop, and to print the result. It is a first example of an accumulator: a variable that starts at a known value and collects a little more on every pass of a loop.
The answer is a single line of output, 2610. You can check it by hand. The range holds 60 numbers, and pairing the smallest with the largest gives 14 + 73 = 87, 15 + 72 = 87 and so on. Thirty pairs of 87 make 2,610.
The building blocks
Variables with var
Kotlin has two keywords for declaring a variable. val creates a read-only name that can't be assigned again, and var creates one that can. An accumulator changes on every pass, so it must be a var:
var sum = 0
sum = sum + 5
sum += 5
After these lines sum is 10. The last line is shorthand for the one before: sum += 5 means sum = sum + 5. Kotlin works out the type from the initial value, so var sum = 0 is an Int and no type needs to be written.
Ranges
The expression 14..73 is a range that includes both 14 and 73. Kotlin has other forms:
0 until 4includes 0, 1, 2 and 3 but stops before the end value;73 downTo 70counts down through 73, 72, 71 and 70;0..10 step 2takes every second number.
Choosing between .. and until is the most common source of off-by-one errors. Here the task says "from 14 to 73", so both ends belong in the sum, and .. is the right choice.
The for loop
for (i in 14..73) runs its body once for each number in the range, and i holds that number during the pass. The loop variable is read-only inside the body, and a new one is made for each pass, so you don't increment it yourself.
The exercise step by step
- Declare the accumulator.
var sum = 0starts the total at zero, because nothing has been added yet. - Write the loop. Iterate with
iover14..73so that every number in the task's range is visited once. - Add inside the loop.
sum += iadds the current number to the running total. - Print after the loop. The
printlngoes after the closing brace of the loop. Then it runs once, when the total is complete.
var sum = 0
for (i in 14..73) {
sum += i
}
println(sum)
This prints:
2610
Common mistakes
- Printing inside the loop. If
println(sum)is inside the braces, the program prints sixty partial sums, starting with14and29, and ending with the right answer on the last line. The output has to be one line. - Using
valfor the total. A read-only name can't be reassigned, sosum += iwould be rejected by the compiler. - Using
until.14 until 73leaves out 73 and gives 2537, which is 2610 minus 73. - Not starting from zero. Starting the total at any other number shifts the answer by that amount.
- Writing the range backwards.
73..14is an empty range in Kotlin, because..only counts upward. The loop body never runs and the program prints0. To count down, usedownTo. - Overflow in bigger sums. An
Intholds values up to 2,147,483,647 and wraps around silently beyond that. For small ranges like this one it doesn't matter, but a sum over a very large range needs aLong, declared asvar sum = 0L.
Key takeaways
- An accumulator is a
varinitialised before the loop and updated inside it. a..bincludes both ends, whilea until bexcludesb.- Print once, after the loop, when you want only the final value.
x += yis a shorthand forx = x + y.