# Factorial or Product, need explanations

#1

Hi,

Can someone please explain me the meaning of the following code:

• I don't get why one should set the variable fact to 1 on line 2
• I don't understand the logic behind fact *= i and i+= 1 on line 4 and 5

I guess it has something to do with my lack of skills in math...

`

#2

`*=` and `+=` are compound operators for multiplication assignment and addition assignment.

``fact = 1``

is initialized to unity since it has no effect on multiplying. It gives us a starting factor to multiply the rest against.

``f *= n``

When f is 1, the assignment after multiplying will make f equal to n. As the loop progresses, the new value for `n` will continue to increase the value of `f`.

#3

That I understand... but why doing this ?

#4

Compound operators are step saving devices.

``````i += 1

# is the same as,

i = i + 1

f *= n

# is the same as,

f = f * n``````

#5

Thank you very much for your quick and comprehensive answer, I think i get it now !
You guys are the best.

#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.