Why the output is 17 not 13 ? Explain in brief? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Why the output is 17 not 13 ? Explain in brief?

int result = 0; for (int i = 0; i < 5; i++) { if (i == 3) { result += 10; } else { result += i; } } System.out.println(result); why the loop is not stop when the condition is met i==3.

6th Sep 2018, 6:17 AM
Nitin Gutte
Nitin Gutte - avatar
2 Answers
+ 5
Let us go step by step: result: 0 i: 0 result += 0 => result: 0 , result: 0 i: 1 result += 1 => result: 1 , result: 1 i: 2 result += 2 => result: 3 , result: 3 i: 3 result += 10 => result: 13 , result: 13 i: 4 result += 4 => result: 17 , print 17 That's it!
6th Sep 2018, 6:50 AM
$machitgarha
$machitgarha - avatar
+ 5
The question I have is - why do you think the loop should stop?
6th Sep 2018, 6:48 AM
Dan Walker
Dan Walker - avatar