need code break down explaination | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

need code break down explaination

$arg = 5; $result = 0; for($i=0; $i<$arg; $i++) { $result = $result + 4 ; } echo $result; how is this outputting 20 ?

10th Mar 2017, 5:58 PM
Hussain Mohammed
Hussain Mohammed - avatar
2 Answers
+ 3
number of times the for loop runs inital value of result is 0 for (5 times){ 4 is added to result } result is now 20 (4+4+4+4+4)
10th Mar 2017, 6:07 PM
seamiki
seamiki - avatar
+ 2
The for loop runs 5 times, because $i takes values 0, 1, 2, 3 and 4. In each of these 5 iterations, you add 4 to $result, which had an initial value of 0. So finally $result is equal to 0+5*4=20.
10th Mar 2017, 6:07 PM
Álvaro