Metadata

  • Date :: 11-04-2025
  • Tags :: cpp

Notes

Understanding Logical Operators: AND, OR, and NOT

Logical operators are fundamental in programming because they allow you to combine multiple conditions, evaluate them, and make decisions based on whether those conditions are true or false. In most programming languages like C++, there are three primary logical operators: AND (&&), OR (||), and NOT (!). These operators are used to form logical expressions and are essential for controlling the flow of a program.

1. AND Logical Operator (&&)

The AND logical operator is used to combine two or more conditions. Both conditions must be true in order for the entire expression to evaluate as true. If one of the conditions is false, the entire expression becomes false, and the code inside the block won’t execute.

Syntax:

condition1 && condition2
 

Both condition1 and condition2 need to be true for the result to be true.

Example: Checking Temperature Range

In this example, we ask the user to input a temperature, and we want to check if the temperature is within a specific range—between 0 and 30 degrees Celsius.

int temp;
cout << "Enter the temperature: ";
cin >> temp;
 
if (temp > 0 && temp < 30) {
    cout << "The temperature is good." << endl;
} else {
    cout << "The temperature is bad." << endl;
}
 
  • Explanation: The if statement checks if both conditions are true:
    • temp > 0 checks if the temperature is greater than 0.
    • temp < 30 checks if the temperature is less than 30.
  • If both conditions are true, the message "The temperature is good." will be displayed.
  • If either of the conditions is false (e.g., the temperature is below 0 or above 30), the message "The temperature is bad." will be displayed.

Test cases:

  • If the temperature is 25, both conditions are true (25 > 0 and 25 < 30), so the output is "The temperature is good."
  • If the temperature is 100, the condition temp > 0 is false, so the output is "The temperature is bad."
  • If the temperature is 100, the condition temp < 30 is false, so the output is "The temperature is bad."

2. OR Logical Operator (||)

The OR logical operator is used when you want to check if at least one of multiple conditions is true. If either of the conditions evaluates to true, the entire expression becomes true. If both conditions are false, the expression evaluates to false.

Syntax:

condition1 || condition2
 

If either condition1 or condition2 is true, the result is true.

Example: Using OR for Temperature Check

This time, let’s check if the temperature is either below 0 or above 30. If the temperature is either too cold or too hot, the result will be "The temperature is bad."

int temp;
cout << "Enter the temperature: ";
cin >> temp;
 
if (temp <= 0 || temp >= 30) {
    cout << "The temperature is bad." << endl;
} else {
    cout << "The temperature is good." << endl;
}
 
  • Explanation: The condition checks if either:
    • temp <= 0 (the temperature is less than or equal to 0), or
    • temp >= 30 (the temperature is greater than or equal to 30).
  • If either of these conditions is true, it prints "The temperature is bad."
  • If neither condition is true (i.e., the temperature is between 0 and 30), it prints "The temperature is good."

Test cases:

  • If the temperature is 25, neither condition is true (25 <= 0 is false and 25 >= 30 is false), so the output is "The temperature is good."
  • If the temperature is 100, the condition temp <= 0 is true, so the output is "The temperature is bad."
  • If the temperature is 100, the condition temp >= 30 is true, so the output is "The temperature is bad."

3. NOT Logical Operator (!)

The NOT logical operator is used to invert the logical state of its operand. If a condition is true, it becomes false, and if a condition is false, it becomes true. The NOT operator is often used when you want to check if something is not true.

Syntax:

!condition
 

If condition is true, !condition becomes false, and if condition is false, !condition becomes true.

Example: Checking Weather Condition (Sunny or Cloudy)

Let’s use a boolean variable sunny to check if the weather is sunny. If it’s sunny, we display "It is sunny outside", and if not, we display "It is cloudy outside".

bool sunny = true;
if (sunny) {
    cout << "It is sunny outside." << endl;
} else {
    cout << "It is cloudy outside." << endl;
}
 
  • Explanation: If sunny is true, the output will be "It is sunny outside."
  • If sunny is false, the output will be "It is cloudy outside."

Now, let’s use the NOT operator to check if it’s not sunny:

bool sunny = false;
if (!sunny) {
    cout << "It is cloudy outside." << endl;
} else {
    cout << "It is sunny outside." << endl;
}
 
  • Explanation: The !sunny checks if sunny is false. Since sunny is false, !sunny becomes true, and it prints "It is cloudy outside."

Test cases:

  • If sunny = true, the output will be "It is sunny outside."
  • If sunny = false, the output will be "It is cloudy outside."

Combining AND, OR, and NOT

You can also combine these operators to form more complex logical conditions. For instance, you might check if the weather is sunny and the temperature is within a certain range to decide if it’s a good day for a picnic:

bool sunny = true;
int temp = 25;
if (sunny && temp > 20 && temp < 30) {
    cout << "Perfect day for a picnic!" << endl;
} else {
    cout << "Not a good day for a picnic." << endl;
}
 
  • Explanation: The if statement checks if:
    • sunny is true (it is sunny),
    • temp > 20 (the temperature is greater than 20),
    • temp < 30 (the temperature is less than 30).
  • All three conditions must be true for the message "Perfect day for a picnic!" to be printed.

Summary of Logical Operators:

  1. AND (&&):
    • Returns true only if both conditions are true.
    • Example: (temp > 0 && temp < 30).
  2. OR (||):
    • Returns true if at least one condition is true.
    • Example: (temp <= 0 || temp >= 30).
  3. NOT (!):
    • Reverses the logical state of the condition.
    • Example: !sunny (if sunny is false, it becomes true).

Conclusion:

Logical operators—AND, OR, and NOT—are essential tools in programming to make decisions based on multiple conditions. They allow you to combine conditions and perform more complex checks in a concise manner. Using these operators effectively helps in writing clean, readable, and efficient code.


References