0

Pregunta

function func($arg) { $result=0 for ($i=0; $i <$arg; $i++) { $result=$result+2; } return $result; } echo func(5); Porque todo esto da un resultado de 10 si result=0 y solo le sumo 2

14th Nov 2016, 11:31 PM
Cesar Augusto RondĂłn
Cesar Augusto RondĂłn - avatar
1 Answer
+ 1
because the loop goes for 5 times and you add to result its previous value's i = 0 -> result = 0 + 2; adding 1 to i i = 1 -> result = 2 + 2; adding 1 to i i = 2 -> result = 4 + 2; adding 1 to i i = 3 -> result = 6 + 2; adding 1 to i i = 4 -> result = 8 + 2; adding 1 to i i = 5 now i is not < 5 so return result that is 10
15th Nov 2016, 2:17 AM
Maurizio Urso
Maurizio Urso - avatar