Relational and Logical Operators in C++
C++ comparison and logical operators form conditions whose results have type bool. In Boolean contexts, arithmetic zero converts to false, while nonzero values convert to true.
Summary
C++ comparison and logical operators form conditions whose results have type bool. In Boolean contexts, arithmetic zero converts to false, while nonzero values convert to true.
| Operator category | Operators | Purpose |
|---|---|---|
| Relational | <, <=, >, >= | Compare ordering |
| Equality | ==, != | Test equality or inequality |
| Logical | AND, OR, NOT | Combine or negate conditions |
Logical AND (&&) requires both operands to be true; logical OR (||) requires at least one true operand; logical NOT (!) reverses a truth value.
Built-in && and || evaluate left to right and short-circuit: && skips its right operand when the left operand is false, while || skips its right operand when the left operand is true. This supports guards such as ptr != nullptr && ptr->ready(), but also means right-hand side effects may not occur.
Precedence, highest to lowest, is !, relational operators, equality operators, &&, then ||. Thus, a || b && c groups as a || (b && c). Explicit parentheses clarify compound conditions used in validation, branching, and loop termination.
Common Misconceptions: == compares values; = assigns a value. An inclusive range requires low <= x && x <= high, not low <= x <= high. Short-circuited operands are not evaluated.
🧠 Key Concepts
- Relational operators
- Equality operators
- Boolean conversion
- Logical operators
- Short-circuit evaluation
- Operator precedence
- Inclusive range tests
- Pointer guards
🧠 Quick Check
See what you remember from the summary.
When an arithmetic value is used in a C++ Boolean context, how is it converted?
🧠 Flashcards Preview
Tap a card to reveal the definition.
Ready to quiz yourself?
Test what you remember with a full practice quiz on this note. Create a free account and start in seconds.
Full Notes
Read the original note content before deciding whether to save or study from it.
Relational and Logical Operators in C++
📘 Overview Relational and logical operators form conditions that evaluate to bool values: true or false. Comparisons test relationships between values, while logical operators combine or negate conditions.
🧠 Key Idea Build compound conditions from explicit comparisons using &&, ||, and !. Built-in && and || evaluate left to right and skip the right operand when the left operand already determines the result.
⚔️ Core Details: - Comparison operators include relational operators <, <=, >, >= and equality operators ==, !=; their results have type bool. - In Boolean contexts, arithmetic zero converts to false and nonzero values convert to true. - Built-in logical operators use Boolean operands: && requires both to be true, || requires at least one to be true, and ! reverses the truth value. - Short-circuit evaluation skips the right operand of built-in && when the left operand is false, and of built-in || when the left operand is true. - Precedence from highest to lowest is !, relational operators, equality operators, &&, then ||. - The == operator tests equality; the = operator assigns a value.
🎯 Why It Matters: - Compound conditions express input validation, loop termination, and branching decisions without deeply nested control structures. - A guard such as ptr != nullptr && ptr->ready() prevents dereferencing a null pointer through short-circuit evaluation. - Explicit parentheses clarify mixed logical conditions and reduce mistakes caused by precedence assumptions. - Side effects in a right-hand operand may never occur because short-circuit evaluation can skip that operand.
🧠 Quick Recall: - Relational operators - < means less than; <= means less than or equal; > means greater than; >= means greater than or equal. - Equality operators - == tests equality; != tests inequality. - Logical operators - && is logical AND; || is logical OR; ! is logical NOT. - Precedence example - a || b && c groups as a || (b && c). - Range test - low <= x && x <= high checks an inclusive range; low <= x <= high instead compares the first comparison's bool result with high.
More ways to study when you copy this note
Copy this note into your library to unlock focused practice sessions and long-term review.
Answer all questions first, then see feedback at the end — the way real exams work.
Focuses each session on what you got wrong, not what you already know.
Full timed exam with all questions, no pausing, and results at the end. Built for board exam prep.
Part of 🗄️ BS Information Systems — 1st Year
Browse this Review Set →More Computer Science notes
See all →More in Programming Fundamentals
See all →More from NoteLib
Browse NoteLib's public notes →Copy this note to your library and get the full Study Pack instantly — summary, key concepts, and practice quiz included.