C++

Relational and Logical Operators in C++

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).

1. Relational Operators

Relational operators are used to compare two operands. They determine the relationship between the values on the left and right sides of the operator.

The Theory: Comparison and Truth

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.

OperatorMeaningExampleResult (if a=5, b=10)
==Equal toa == bfalse
!=Not equal toa != btrue
>Greater thana > bfalse
<Less thana < btrue
>=Greater than or equal toa >= 5true
<=Less than or equal tob <= 10true

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.

2. Logical Operators

Logical operators are used to combine two or more relational expressions or to negate a condition. They are the "connectives" of logic.

A. Logical AND (&&)

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.

B. Logical OR (||)

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.

C. Logical NOT (!)

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.

3. Short-Circuit Evaluation (Deep Theory)

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.

4. Practical Code Example

#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;
}

5. Operator Precedence (The Hierarchy)

When you mix arithmetic, relational, and logical operators, C++ follows this order of operations:

  1. Logical NOT (!) and Unary operators.

  2. Arithmetic Operators (*, /, then +, -).

  3. Relational Operators (<, >, <=, >=).

  4. Equality Operators (==, !=).

  5. Logical AND (&&).

  6. Logical OR (||).

Recommendation: Always use parentheses () to make your logic clear and avoid precedence confusion.

6. Summary Table for EMBLAb

CategoryOperatorsResult TypeShort-Circuit?
Relational==, !=, <, >, <=, >=boolNo
Logical&&, ``
Negation!boolNo
Upcoming Course
Upcoming Course
Learn More
Instructor Tips
Instructor Tips
View Tips
Join Community
Join Community
Join Now