Sum

So far, we’ve just used one mutable variable to keep track of where we are in the loop. But we’re not limited to that. We can also use a mutable variable to keep track of a result. This is sometimes known as an accumulator since we’re accumulating a result value at each iteration. We can use this technique to add the numbers from 1 to 10.

Get hands-on with 1400+ tech skills courses.