In C++, Relational and Logical operators are the building blocks of decision-making. They allow a program to compare values and combine multiple conditions to control the flow of execution. These operators always evaluate to a Boolean value: either true (1) or false (0).
Relational operators are used to compare two operands. They determine the relationship between the values on the left and right sides of the operator.
In C++, any non-zero value is technically considered "true," but relational operators specifically return the bool constants true or false. These are primarily used in if statements and loops to decide whether a block of code should run.
| Operator | Meaning | Example | Result (if a=5, b=10) |
| == | Equal to | a == b | false |
| != | Not equal to | a != b | true |
| > | Greater than | a > b | false |
| < | Less than | a < b | true |
| >= | Greater than or equal to | a >= 5 | true |
| <= | Less than or equal to | b <= 10 | true |
Critical Theory: = vs ==
One of the most common bugs in C++ is using the assignment operator (=) instead of the equality operator (==).
if (x = 5) assigns 5 to x. Since 5 is non-zero, the condition is always true.
if (x == 5) checks if x is equal to 5. This is the correct way to compare.
Logical operators are used to combine two or more relational expressions or to negate a condition. They are the "connectives" of logic.
Returns true only if both operands are true.
Theory: If the first condition is false, C++ does not even check the second one. This is called Short-Circuit Evaluation.
Example: (5 > 3 && 10 > 5) is true.
Returns true if at least one of the operands is true.
Theory: If the first condition is true, the second is skipped because the overall result is already guaranteed to be true (Short-Circuiting).
Example: (5 > 10 || 10 > 5) is true.
A unary operator that reverses the logical state of its operand. If a condition is true, ! makes it false.
Example: !(5 > 10) is true because it negates a false statement.
Short-circuiting is a compiler optimization that improves performance and prevents errors.
In &&: If the left side is false, the whole expression is false. C++ stops there.
In ||: If the left side is true, the whole expression is true. C++ stops there.
Why is this important? Imagine you have a condition: if (y != 0 && x / y > 2).
If y is 0, the first part (y != 0) is false. Because of short-circuiting, the second part (x / y) is never executed, preventing a "Division by Zero" crash.
#include <iostream> using namespace std; int main() { int age = 20; bool hasLicense = true; bool isSober = true; cout << boolalpha; // Prints 'true'/'false' instead of 1/0 // Relational Operators cout << "Is age greater than 18? " << (age > 18) << endl; cout << "Is age exactly 21? " << (age == 21) << endl; // Logical Operators // Requirement: Must be over 18 AND have a license AND be sober if (age >= 18 && hasLicense && isSober) { cout << "Decision: You are allowed to drive." << endl; } else { cout << "Decision: You cannot drive." << endl; } // Logical OR and NOT bool isHoliday = false; bool isWeekend = true; if (isHoliday || isWeekend) { cout << "Status: The office is closed." << endl; } if (!isHoliday) { cout << "Reminder: It is a working day unless it's the weekend." << endl; } return 0; }
When you mix arithmetic, relational, and logical operators, C++ follows this order of operations:
Logical NOT (!) and Unary operators.
Arithmetic Operators (*, /, then +, -).
Relational Operators (<, >, <=, >=).
Equality Operators (==, !=).
Logical AND (&&).
Logical OR (||).
Recommendation: Always use parentheses () to make your logic clear and avoid precedence confusion.
| Category | Operators | Result Type | Short-Circuit? |
| Relational | ==, !=, <, >, <=, >= | bool | No |
| Logical | &&, ` | ` | |
| Negation | ! | bool | No |
Copyright ©2025. All Rights Reserved Emblab THE RAVE INNOVATION