01 / 21

Avoiding Conditional
Bugs in Java

Master the subtle traps in if/else logic, from operator ordering to boundary conditions, and write conditionals that work correctly every time.

Java

What Is a Conditional Bug?

A conditional bug occurs when an if, else if, or else statement does not behave the way the programmer intended. The code compiles and runs, but it produces the wrong result for certain inputs.

Why Conditionals Go Wrong

Operator Errors
Using \( = \) instead of \( == \), or confusing \( < \) with \( \leq \)
Order Errors
Placing conditions in the wrong sequence so an earlier branch shadows a later one
Boundary Errors
Off-by-one mistakes at the edges of a range: \( \leq \) vs \( < \)

How Java Evaluates Conditionals

if (condition1) true? true body1 false else if (cond2) true? body2 else

The Golden Rules of Conditionals

1
Most specific first. Place narrower conditions before broader ones so they are not shadowed.
2
Test both sides of every boundary. If a threshold is \( n \), test with \( n-1 \), \( n \), and \( n+1 \).
3
Use \( == \) for equality, \( = \) for assignment. Never confuse the two in a boolean context.
4
Verify mutual exclusion. Ensure your branches cover all cases without overlap (unless intentional).

\( == \) vs \( = \) : The Classic Trap

✕ Bug
if (score = 100) { System.out.println("Perfect!"); }
✓ Fixed
if (score == 100) { System.out.println("Perfect!"); }

Boundary Detail: \( < \) vs \( \leq \)

A grading program awards a B for scores from 80 to 89 (integer). Both conditions below are equivalent for integers:

✓ Option A
if (score >= 80 && score < 90) grade = "B";
✓ Option B
if (score >= 80 && score <= 89) grade = "B";

The Shadowing Bug

When a broader condition is placed before a narrower one, the narrow branch can never execute.

✕ Bug: broad first
if (score >= 60) { grade = "Pass"; } else if (score >= 90) { grade = "Honor Roll"; }
✓ Fixed: narrow first
if (score >= 90) { grade = "Honor Roll"; } else if (score >= 60) { grade = "Pass"; }

Visualizing Boundary Conditions

0 60 90 100 F (0-59) Pass (60-89) Honor boundary boundary score = 95 Test this value!

Bug-Proofing Checklist

1
List all distinct outcomes before writing any code. What should happen for each range of input?
2
Order from most specific to least specific. Narrow conditions first, broad catch-all (else) last.
3
Choose operators deliberately. Decide for each boundary: does it belong to the lower range or the upper range?
4
Trace with boundary values. Mentally (or on paper) run the code with each boundary value and its neighbors.
5
Check for gaps and overlaps. Every valid input should match exactly one branch.

Example 1: Ticket Pricing

A theater charges: Child (age \( < \) 13): $8, Teen (age 13-17): $12, Adult (age \( \geq \) 18): $15. Find the bug:

if (age < 18) { price = 12; // Teen price } else if (age < 13) { price = 8; // Child price -- SHADOWED! } else { price = 15; // Adult price }

Example 1: Fixed Ticket Pricing

if (age < 13) { price = 8; // child: 0-12 } else if (age < 18) { price = 12; // teen: 13-17 } else { price = 15; // adult: 18+ }

Example 2: Temperature Warnings

A weather app classifies temperature: Freezing (\( \leq 32 \)), Cold (33-50), Mild (51-75), Hot (\( > 75 \)). Find two bugs:

if (temp <= 50) { msg = "Cold"; } else if (temp <= 32) { msg = "Freezing"; // BUG 1: shadowed } else if (temp < 75) { msg = "Mild"; // BUG 2: temp=75 is "Mild"? } else { msg = "Hot"; }

Example 2: Fixed Temperature

if (temp <= 32) { msg = "Freezing"; // narrowest first } else if (temp <= 50) { msg = "Cold"; // 33-50 } else if (temp <= 75) { msg = "Mild"; // 51-75 (includes 75) } else { msg = "Hot"; // 76+ }

Common Mistakes to Avoid

✕ Missing else
Using separate if statements instead of else if causes multiple branches to execute for the same input.
✕ String \( == \) comparison
Using \( == \) on Strings compares references, not content. Always use .equals() for String values.
✕ Dangling else
Without braces, else binds to the nearest if, which may not be what you intended. Always use { }.

Bug Type Summary

Bug Type Symptom Fix
Shadowed branch A branch never executes Narrow conditions first
Off-by-one Boundary value goes to wrong branch Use \( < \) vs \( \leq \) deliberately
\( = \) vs \( == \) Assignment instead of comparison Always use \( == \) for equality
Missing else Multiple branches fire Chain with else if
String \( == \) Reference comparison fails Use .equals() method

Question 1 of 5

What is the output when score = 95?

Question 2 of 5

Which operator should replace the ? so that exactly the values 1 through 10 print "In range"?

Question 3 of 5

What happens when this code runs with found initially set to false?

Question 4 of 5

A program categorizes ages: Child (0-12), Teen (13-17), Adult (18+). Which code is correct?

Question 5 of 5

What is the best first step when debugging a conditional that produces wrong output for some inputs?