+ 1

Why the output is 2 ?

Why the output is 2?? public class test { public static void main(String [] args) { int x=0; for(int z =0 ;z<5;z++) { if((z>2) && (++x>2)) { x+=7; }} System.out.print(x); } }

1st Jan 2018, 12:00 PM
Rishabh
Rishabh - avatar
2 Answers
+ 3
for z=3 z>2 but ++x=1 is not greater than 2 for z=4 z>2 but ++x=2 is not greater than 2 for z=5 loop terminates.. and finally x=2 and gets printed..
1st Jan 2018, 12:48 PM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
+ 3
@thanks Sami
1st Jan 2018, 12:58 PM
Rishabh
Rishabh - avatar