So what I don’t understand is how he picks int result = 1; out of nowhere? Why not = 2?
That is so it’s not always 0 if he multiplies the result in the for loop. 0 * 2 –> 0
About the for loop itself:
- Iteration: 1 * 2 –> 2 / i = 0
- Iteration: 2 * 2 –> 4 / i = 1
- Iteration: 4 * 2 –> 8 / i = 2
- Iteration: i = 3 / is not smaller than 3 leaves the for-loop
At the end he leaves the function and returns the result which is now 8.
For the for-loop, getting the the power of 3 with a base of 2, gives back the same as if you would just multiple 2 * 2 * 2.
That is exactly what the for-loop does you go through and do the first multiplication 1 * 2, on the second iteration you calculate you multiply the result again 2 * 2, then you go through again and calculate the next iteration 4 * 2 which then gets you the desired result