C++ from Scratch to Advance / Control Statements in C++ / Logical Operators in Conditional Statements
Home /C++ from Scratch to Advance /Control Statements in C++ /Logical Operators in Conditional Statements

Logical Operators in Conditional Statements

In our previous lessons, we used nested if statements to handle complex decisions. While effective, this can lead to lengthy, inefficient code. Today, we will learn about Logical Operators, which allow us to combine multiple conditions into a single line, making our code shorter and more professional.

Definitions:

Term Definition

Example

Logical Operators Tools used to combine two or more relational expressions (comparisons) into one.
  •         AND operator (&&)
  •         OR operator (||)
  •         NOT operator (!)
Binary Operator An operator that requires two inputs (operands) to work, such as && and ||.
  •          Assignment operator (=)
  •          Arthimatic operators (+,  -, *, /, %)
  •           Logical operators
AND Operator (&&) A rule where the result is Trueonly if every single part is true.
  •       5==5 && 4!=5 (true)
  •       5>5 && 5>10 (false)
  •       5>10 and 4<5 (false)
OR Operator (||) A rule where the result is True if at least one part is true.
  •       5==5 || 4!=5 (true)
  •       5>5 || 5>10 (false)
  •       5>10 || 4<5 (true)
NOT Operator (!) An operator that reverses the result (True becomes False and vice versa).
  • !( 5==5) (false)
  • !true (false)
  • !false (true)
Short-Circuit Evaluation A time-saving feature where the computer stops checking conditions as soon as the final result is certain.
  • 5>10 && 7<19 (false):  When the first part is false (5>10) it will not check the rest part as and operator (&&) is used between the two conditions.

Explanation

The AND Operator (&&)

The AND operator is used when you have multiple requirements that must all be met. In C++, it is represented by a double ampersand &&.

  • How it Works: It takes a left-side expression and a right-side expression. If both are true, the whole thing is true.
  • Short-Circuiting: If the left side is False, C++ won’t even check the right side. Why? Because if one part is false, the AND result is guaranteed to be false, so the computer saves time by stopping early.

Relatable Example: To graduate, you must pass your exams AND pay your academic fee. If you fail either one, you don’t graduate.

Truth Table of AND (&&) Operator

A

B A&&B

true

true true

true

false

false

false true

false

false false

false

The OR Operator (||)

The OR operator is used when you have multiple options, and any one of them being true is enough. It is represented by two straight lines ||.

  • How it Works: If the left side is true OR the right side is true, the result is true. It only returns false if every part is false.
  • Short-Circuiting: If the left side is True, C++ stops checking immediately. Since one “True” is enough for the OR operator, the second part doesn’t matter.

Relatable Example: You can enter the cinema if you have a physical ticket OR a digital QR code on your phone.

Truth Table of OR (||) Operator

A

B A||B
true true

true

true false

true

false true

true

false false

false

The NOT (!) Operator

The ! (NOT) operator is one of the three primary logical operators in C++. While the AND and OR operators are “binary operators” (meaning they require both a left and right expression to work), the NOT operator is the exception to this rule. The source notes that a detailed explanation and implementation of the NOT operator is reserved for the next lecture in the series.

Truth Table of NOT (!) Operator

A

!A

true

false

false

true

Common Pitfalls: Incomplete Expressions

A common mistake is forgetting to repeat the variable name in combined expressions.

Invalid: if (temp < 20 || > 100) — The computer doesn’t know what to compare to 100.

Valid: if (temp < 20 || temp > 100) — Both sides are complete and clear.

Short-Circuit Evaluation

Short-circuit evaluation is a method the compiler uses to save time and reduce resource consumption when checking logical expressions.

AND (&&) Short-Circuiting: If the left side of an AND expression is False, the compiler skips checking the right side. Since both sides must be true for an AND expression to succeed, a false result on the left guarantees the entire expression is false.

OR (||) Short-Circuiting: If the left side of an OR expression is True, the compiler skips checking the right side. Because an OR expression only needs one true value to succeed, the entire expression is already guaranteed to be true

Example Problems

Example 1: Age Range (AND)

This program checks if a user is eligible for a “Young Adult” discount (ages 18 to 25).

#include <iostream>
using namespace std;
int main() {
    int age;
    cout << "Enter your age: ";
    cin >> age;
    if (age >= 18 && age <= 25) {
        cout << "You are eligible for the discount!" << endl;
    } 
    else {
        cout << "Sorry, no discount for your age group." << endl;
    }
    return 0;
}

Example 2: Login System (OR)

This program allows entry if the user provides either a valid Username or a Guest Code.

#include <iostream>
using namespace std;
int main() {
    bool hasUsername = true;
    bool hasGuestCode = false;
    if (hasUsername == true || hasGuestCode == true) {
        cout << "Access Granted!" << endl;
    } 
    else {
        cout << "Access Denied. Please provide identification." << endl;
    }
    return 0;
}

Try It Yourself!

  1. Truth Table Test: If A is False and B is True, what is the result of (A && B)? What about (A || B)?
  2. Short-Circuit Challenge: In the expression if (5 > 10 && 20 > 10), does the computer ever check if 20 > 10? Why?
  3. Code Fixer: Fix this statement: if (score == 10 || 20).
  4. Real-World Logic: Write a single if statement for this rule: “You can go outside if it is not raining AND you have finished your homework.”
  5. Multi-Condition: Can you use two AND operators to check three things at once? (e.g., age > 10 && age < 20 && height > 5).

Next Steps

Logical operators are the key to writing clean, professional code. For more details on operator precedence, visit the Official C++ Documentation.

Engagement Boost: What is your biggest C++ fear—is it understanding the logic of AND/OR, or just remembering to use double symbols like &&? Let us know in the comments!