Procedural Programming with C++
Explainer
Why C++ Sometimes Skips Half Your Condition
In C++, the right side of && may never run at all.
Quick check · tap an answer to continue
Lesson notes
Key points
- The logical operators && and || evaluate left to right and stop as soon as the result is already decided.
- This lets you guard risky checks, like testing i < n before reading arr[i], preventing an out-of-bounds read.
- Reversing the order to arr[i] == key && i < n reads memory first, so the protection is lost.
Takeaway: && and || stop early, so order your conditions deliberately.
Quick check: In (x != 0 && 10 / x > 2), if x is 0, what happens?
- Division by zero crash
- 10 / x is skipped
- Compile error
Short-circuitGuard conditionOrder matters
Lessons like this, for your syllabus.
Short narrated study videos written for your course, level and exam. Free to start.
Get lessons for your own syllabus