+ 1

why is the answer not 1; for the description belonw

it result =0 for (int=0; <5;i++){ if (i==3){ result + =10; }else { result + =i; } } system.out.print|n (result);

22nd Sep 2016, 2:46 PM
Kyei Moses
Kyei Moses - avatar
1 Answer
+ 2
The for loop indicates that it loops from index 0-4. Loop 1: i = 0, so the total is 0 + i = 0. Loop 2: i = 1, so the total is 0 + i = 1. Loop 3: i = 2, so the total is 1 + i = 3. Loop 4: i = 3, this is where the if statement is true so the total is now 3 + 10 = 13. Loop 5: i = 4, so the total is 13 + 4 = 17.
22nd Sep 2016, 7:53 PM
Parash Patel
Parash Patel - avatar