Java Code Execution Tables
1. for Loop with Multiplication After Loop
Code:
int y;
for(y = 1; y <= 10; y += 2)
System.out.print(y + " ");
System.out.println("\n" + y * 10);
Step Code Executed Working / Value of y Output
Reason
1 y=1 Initialization of 1
loop variable
2 y <= 10 Condition is 1
true, so body
executes
3 System.out.prin Prints current 1 1
t(y + " ") value of y
4 y += 2 Increment y by 3
2
5–9 Repeat steps All satisfy - 3579
for y = 3, 5, 7, 9 condition y <=
10 and are
printed
10 y = 11 → loop 11 > 10, 11
ends condition
becomes false
11 System.out.prin After loop, y = 11 110
tln("\n" + y * 11, so 11 * 10 =
10) 110 is printed
on new line
Final Output:
1 3 5 7 9 (same line)
110 (on next line)
2. if(n > 0) Condition on Variables x and y
Code:
x = 1;
y = 1;
if(n > 0) {
x = x + 1;
y = y - 1;
}
Case Value of n Condition (n Working / x y / Output
> 0) Reason
1 1 true Since n > 0, 2 0
both
statements
inside the if
block
execute
2 0 false Since n is 1 1
not greater
than 0, the
block is
skipped;
values of x
and y stay
the same
3. do-while Loop Starting with a = -8
Code:
int a = -8;
do {
System.out.println(a);
a++;
} while(a < 0);
Step Code Executed Working / Reason Value of a / Printed
1 do { → do-while ensures -8 → -8
System.out.println(a the loop executes at
) least once
2 a++ Increments a by 1 -7
3–9 Loop continues Repeats with a = -7 -7 to -1
while a < 0 to a = -1 since
condition remains
true
10 a = 0, condition fails 0 < 0 is false → loop (loop ends)
ends
Final Output (each on new line):
-8
-7
-6
-5
-4
-3
-2
-1