for loops | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

for loops

please help: why do I get different outputs from this code: 1. int i; for(i = 0;i<100;i++); System.out.println(i); 2. int i; for(i = 0;i<100;i++){ System.out.println(i) ; } please help, how does the insertion of semicolon and curled brackets affect the output..?

9th Dec 2016, 4:42 AM
Emmett N. Lipholo
Emmett N. Lipholo - avatar
3 Answers
0
1 : 100 2: 0 to 99
9th Dec 2016, 5:07 AM
Morpheus
Morpheus - avatar
0
in first case there is no loop body so, only i gets incremented , in the end i is 100 it checks and condition doesn't holds, but the value for i is still 100 in second case there is loop body inside {} braces, so till the condition holds it prints 0 till 99 , but not 100 as before entering the loop body condition is checked , and 100 is not< 99. so end of loop
9th Dec 2016, 5:11 AM
Morpheus
Morpheus - avatar
0
bro... this is helpful.. thanks a lot
9th Dec 2016, 9:23 AM
Emmett N. Lipholo
Emmett N. Lipholo - avatar