0

Can anyone explain proper working of this code ? How its working in background !! How it produce "2" output

What is the output of this code? int sum =0; for(int i=2; i<5; i+=2) { if(8 / i == 2) { break; } sum += i; } System.out.print(sum);

9th Jan 2020, 10:26 AM
Nihaal Khan
Nihaal Khan - avatar
1 Antwort
+ 4
Only when 8/i == 2 the break is executed but until then sum+=i will be executed. i=2 & i<5 is true so move inside loop- 8/2 is not equal to 2 so sum+=i is executed and sum becomes 2. i is incremented by 2 so i=4 & 4<5 is true 8/4 is equal to 2 so the break is executed and the control comes out of the loop and prints the sum value 2.
9th Jan 2020, 10:37 AM
Avinesh
Avinesh - avatar