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
6 Antworten
+ 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?
+ 2
this code contains many errors
like capital I in the condition and integer
R in result
but after fixing
answer is 17
+ 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
+ 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)
+ 1
do you mind explaining how the “continue” function works with an example?
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