Why is the output of this code 17 and not 15? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why is the output of this code 17 and not 15?

Int Result = 0; For(int i= 0 ; I < 5 ; i++) { If(i == 3) { continue; Result += 10; } else { Result += i; } System.out.println(result); This was a question in the quiz

30th May 2018, 3:30 PM
met rk
met rk - avatar
6 Answers
+ 2
0 + 1 + 2 + 4 = 7. For 3 apparently we do nothing, unless the "result += 10" should be above the continue (giving 17). Why do you think the output should be 15?
30th May 2018, 3:37 PM
Dan Walker
Dan Walker - avatar
+ 2
this code contains many errors like capital I in the condition and integer R in result but after fixing answer is 17
30th May 2018, 3:43 PM
Programmer Gaurav
Programmer Gaurav - avatar
+ 2
Put simply, a loop begins, does some stuff and then ends the cycle, moving on to the next iteration. when you meet a continue statement, the loop ends immediately and starts the next iteration without executing any more code in that 'run' of the loop
30th May 2018, 6:30 PM
Dan Walker
Dan Walker - avatar
+ 1
I see :) don't forget it's < 5 too, so the loop won't execute for i == 5 as the condition is not true. The variable i is also local to that loop, and won't be visible outside (unless you first defined it outside)
30th May 2018, 4:25 PM
Dan Walker
Dan Walker - avatar
+ 1
do you mind explaining how the “continue” function works with an example?
30th May 2018, 5:36 PM
met rk
met rk - avatar
0
i forgot about the continue and how t works, so i thought after the loop was over the outpt of the loop would be 5 & then result which is 10
30th May 2018, 3:57 PM
met rk
met rk - avatar