Today I explored some tricky and confusing scenarios in Java if / else statements.
At first, if-else looks simpleโฆ but small mistakes in syntax, conditions, or indentation can completely change the output ๐ฒ
So I practiced 12 tricky questions to understand how Java actually executes them.
๐น What is if Statement?
โ Executes code only when condition is true
๐ Scenario 1
int x = 5;
if (x > 2)
System.out.println("Hello");
โ
Explanation
5 > 2 โ true
๐ค Output
Hello
๐ Scenario 2
int x = 5;
if (x < 2)
System.out.println("Hello");
System.out.println("Hi");
โ Explanation
5 < 2 โ false โ if block skipped
But the second statement is outside if, so it executes.
๐ค Output
Hi
๐ Scenario 3
if(true)
{
System.out.println("Hello");
}
โ Explanation
Condition is always true
๐ค Output
Hello
๐ Scenario 4
if(false)
{
System.out.println("Hello");
}
โ Explanation
Condition is always false
๐ค Output
(No Output)
๐ Scenario 5
int x = 5;
if (x > 2)
System.out.println("Hello");
else
System.out.println("Hi");
โ
Explanation
5 > 2 โ true
๐ค Output
Hello
๐ Scenario 6 (Important โ ๏ธ)
int x = 10;
if (x = 20) {
System.out.println("True");
}
โ Explanation
x = 20 โ Assignment, NOT comparison
Java expects boolean condition, but x = 20 returns int.
๐ค Result
Compile Time Error
๐ Correct way:
if (x == 20)
๐ Scenario 7 (Dangling Else Problem)
int x = 5;
if (x > 2)
if (x < 10)
System.out.println("A");
else
System.out.println("B");
โ
Explanation
if (x > 2) โ true
if (x < 10) โ true
๐ else always attaches to the nearest if
๐ค Output
A
๐ Scenario 8
int a = 0;
if (a)
System.out.println("Yes");
else
System.out.println("No");
โ Explanation
`Java does NOT allow:
if(a)
Condition must be boolean`
๐ค Result
Compile Time Error
๐ Scenario 9 (Very Important ๐ฅ)
int x = 0;
if (x++ == 0 && ++x == 2)
System.out.println("True");
System.out.println(x);
โ Explanation
`Step by step:
x = 0
x++ == 0 โ true (x becomes 1)
++x == 2 โ x becomes 2 โ true
Both conditions true โ print "True"`
๐ค Output
True
2
๐ Scenario 10 (Short Circuit OR)
int x = 10;
if (x == 10 || x++ == 11)
System.out.println(x);
โ
Explanation
`x == 10 โ true
๐ OR (||) stops execution if first condition is true
๐ x++ is NOT executed`
๐ค Output
10
๐ Scenario 11 (Common Mistake โ ๏ธ)
int x = 5;
if (x > 2)
System.out.println("A");
System.out.println("B");
โ
Explanation
Only first line is part of if
Second line always executes
๐ค Output
A
B
๐ Scenario 12 (Nested if)
int x = 10, y = 5;
if (x > y)
if (y > 0)
if (x > 20)
System.out.println("A");
else
System.out.println("B");
โ
Explanation
x > y โ true
y > 0 โ true
x > 20 โ false
So else executes.
๐ค Output
B
๐ง Key Takeaways
โ if works only with boolean conditions
โ = is assignment, == is comparison
โ else always binds to nearest if
โ Without {}, only one statement belongs to if
โ Short-circuit operators (&&, ||) can skip execution
โ Increment operators (++) change values during evaluation
๐ Conclusion
Todayโs session helped me understand how small syntax differences can completely change program behaviour.
These tricky questions are very useful for:
โ Java interviews
โ Writing correct automation logic
โ Debugging real-world code
Step by step, Iโm becoming more confident in Java fundamentals for automation testing ๐
๐ค A Small Note
I used ChatGPT to help structure and refine this blog while ensuring the concepts remain aligned with my trainerโs explanations.

Top comments (0)