class MyClass { public static void main(String[ ] args) { int a=0; int even = 0; for(even = 0;even <= 100;++even | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

class MyClass { public static void main(String[ ] args) { int a=0; int even = 0; for(even = 0;even <= 100;++even

why the output is exceeding 100 in above code?

2nd Mar 2017, 2:49 PM
Sahil
Sahil - avatar
1 Answer
0
in a for loop, the first statement is called once,at the beginning. even is then set to 0. The second statement, the condition is always tester BEFORE the code in the loop is testet. The last statement is always called AFTER the code in the loop is executed. So, as you use <= , 100 is the last number its true. But then it still gets incremented once. Then even is 101 , so bigger than 100 and the loop stops
2nd Mar 2017, 5:14 PM
No One
No One - avatar