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)