Meaning of accumulator(?)

I was wondering if someone could explain me the meaning of the accumulator in this code cause I can’t understand the output! here’s the code when I scan 3 as “n” :

package main

import “fmt”

func main() {
var n int

fmt.Scan(&n)

accumulator := 1

for i := 1; i <= n; i++ { 
    switch {
    case i < 5:
        accumulator *= i
    case i < 10:
        accumulator += i
    }
}
fmt.Println("Accumulator=", accumulator)

}

output: 6

I think that there’s something I don’t know about the accumulator

1*2*3 is 6, seems correct, what is it you don’t understand?

1 Like

Okay I got it, so if I put 100 (for example) as “n” what will be the output?

59 if I read the code correctly. Any n greater than or equal to 9 won’t change anything from the output, it will just take longer.

1 Like

but why 59? if any n greater than or equal to 9 won’t change anything from the output, I have to do: 1+2+3+4+5+6+7+8+9 + accumulator but this doesn’t show me 59

I’d expect

1*2*3*4+5+6+7+8+9 (24 + 11 + 15 + 9 = 35 + 24 = 59) due to the condition in the switch checking for i.

Then the current i gets multiplied by or added to the current accumulator and the result reassigned.

Though once i becomes greater than or equal to 10, nothing happens to the accumulator anymore.


Confirms my claim. It prints 59…

1 Like

For functions like this, I think the Print function “family” in the fmt package is extremely useful. I added some calls to fmt.Printf and fmt.Println to your code just to print out what’s going on and I think it makes it easier to understand what’s happening and why:

i: 1	|	accumulator: 1	|	multiplying	1 by 1	|	accumulator: 1
i: 2	|	accumulator: 1	|	multiplying	1 by 2	|	accumulator: 2
i: 3	|	accumulator: 2	|	multiplying	2 by 3	|	accumulator: 6
i: 4	|	accumulator: 6	|	multiplying	6 by 4	|	accumulator: 24
i: 5	|	accumulator: 24	|	adding		5 to 24	|	accumulator: 29
i: 6	|	accumulator: 29	|	adding		6 to 29	|	accumulator: 35
i: 7	|	accumulator: 35	|	adding		7 to 35	|	accumulator: 42
i: 8	|	accumulator: 42	|	adding		8 to 42	|	accumulator: 50
i: 9	|	accumulator: 50	|	adding		9 to 50	|	accumulator: 59
accumulator: 59
accumulator: 59
accumulator: 59
...
Program exited.
2 Likes

thank you so much guys!

I would also add that reviewing code I would take the time to say the following with each statement.

accumulator := 1

We are creating a variable of TYPE INT using the short declaration operator. I know this is obvious but being explicit about each statement will help understand what this accumulator actually does.

Within the for loop we handle some conditional logic such that when i, which is 1 entering the for loop, encounters the first case statement it checks if it is less than 5. It is therefore it will get multiplied by 1 and then assigned back into the accumulator variable. The second case will also be hit because i is less than 10 therefore we add 1 to the accumulator, etc…

I would also recommend you using delve to step through your code as debugging is a great way to learn what is actually going on.

[delve/getting_started.md at master · go-delve/delve · GitHub](Delve - Getting Started)