Java Loop and Conditional Evaluations
1. Loop with for and System.out.println
Step Statement Value of y Output
1 Initialization y=1
2 Check y <= 10 true
3 Print y + " " 1 1
4 Increment y += 2 y=3
... Loop repeats: 3, 5, 7, 3579
9
5 y = 11 → condition
false, exit loop
6 After loop: y = 11 → 11*10 110 (on new line)
System.out.println("
\n" + y*10)
Final Output (for loop)
Line Output
1 13579
2 110
2. Conditional with if(n > 0)
Case Value of n Condition n x becomes y becomes Output
>0
1 1 true 1+1=2 1-1=0 x = 2, y = 0
2 0 false 1 1 x = 1, y = 1
3. Loop with do-while
Step Value of a Condition a < 0 Printed Value Next a
1 -8 true -8 -7
2 -7 true -7 -6
3 -6 true -6 -5
4 -5 true -5 -4
5 -4 true -4 -3
6 -3 true -3 -2
7 -2 true -2 -1
8 -1 true -1 0
9 0 false loop ends —
Final Output (do-while loop)
Line Output
1 to 8 -8 to -1 (each on new line)