Avoiding Conditional
Bugs in C++
Master the art of writing correct conditionals by understanding ordering pitfalls, boundary conditions, and common logical traps.
What Is a Conditional Bug?
A conditional bug occurs when your if/else logic does not match the behavior you intended. The code compiles and often runs without crashing, but it produces wrong results for certain inputs.
Why Conditionals Go Wrong
The Bug Lifecycle
The Golden Rule of Conditionals
Ordering Matters: Shadowing
When a general condition appears before a specific one, the specific branch never executes. This is called shadowing.
\( = \) vs \( == \) and \( < \) vs \( \leq \)
| Bug Version | Correct Version | |
|---|---|---|
| Assignment vs Comparison | if (x \( = \) 5) |
if (x \( == \) 5) |
| Off-by-one: excluding boundary | if (i \( < \) n) when you need to include \( i = n \) |
if (i \( \leq \) n) |
| Off-by-one: including extra | if (i \( \leq \) n) for 0-indexed array |
if (i \( < \) n) |
| Negation confusion | if (\( !x > 0 \)) |
if (\( !(x > 0) \)) |
Boundary Conditions Visualized
The \( && \) vs \( || \) Trap
Confusing AND with OR is one of the most frequent conditional bugs. De Morgan's Laws are your best friend:
The 5-Step Checklist
==, <, <=, &&, || matches your intent.Example 1: Range Check
Write a function that returns "child" for ages 0 to 12, "teen" for 13 to 17, and "adult" for 18 and above.
Example 2: Nested Condition Trap
A ticket system gives a discount to students or seniors. A common bug: nesting the check incorrectly.
Example 3: Short-Circuit Safety
C++ evaluates \( && \) left to right and stops early if the left side is false. You can use this to guard dangerous operations.
The Dangling Else Problem
Without braces, an else binds to the nearest if, not the one you might intend.
Common Mistakes
if (x = 5) instead of if (x == 5). Always assigns and evaluates as true.else fallback leaves inputs unhandled, causing undefined or stale variable values.Quick Reference Table
| Bug Type | Symptom | Prevention |
|---|---|---|
| Shadowing | Specific branch never runs | Order specific to general |
| Off-by-one | Crash or missing last element | Test at boundary values |
| \( = \) vs \( == \) | Condition always true | Enable compiler warnings |
| \( && \) vs \( || \) | Impossible or always-true condition | Apply De Morgan's Laws |
| Dangling else | Else binds to wrong if | Always use braces {} |
| Short-circuit order | Crash on invalid access | Guard check on the left |
Question 1 of 5
What does the expression if (x = 10) do in C++?
= is assignment, not comparison. It sets x to 10, and since 10 is nonzero, the condition is always true. Use == for comparison.Question 2 of 5
Code:
if (score \( \geq \) 60) grade = 'D';
else if (score \( \geq \) 70) grade = 'C';
else if (score \( \geq \) 80) grade = 'B';
else if (score \( \geq \) 90) grade = 'A';
else grade = 'F';
If score = 85, what grade is assigned?
score \( \geq \) 60 is checked first and \( 85 \geq 60 \), the first branch executes and assigns 'D'. The more specific branches are shadowed. Fix: order from highest to lowest threshold.Question 3 of 5
For an array of size \( n \), which loop condition correctly iterates over all valid indices?
Question 4 of 5
You want to check that x is not between 1 and 100 (inclusive). Which condition is correct?
&&, which is impossible (no number is both \( < 1 \) and \( > 100 \)). Option D correctly uses || by De Morgan's Law: \( \neg(x \geq 1 \land x \leq 100) = x < 1 \lor x > 100 \).Question 5 of 5
Code:
if (i \( < \) n \( && \) arr[i] \( == \) target) {
found = true;
}
Why is it important that i < n appears before arr[i] == target?
&& is false, the right side is never evaluated. So when \( i \geq n \), the dangerous arr[i] access is skipped entirely, preventing undefined behavior.