Logical Operators

Estimated reading: 3 minutes 78 Views

Logical operators in C++ are used to combine or modify Boolean expressions. They allow you to create more complex conditional statements.

The three logical operators in C++ are:

  • And (&&): Returns true if both of its operands are true. Otherwise, it returns false.
  • Or (||): Returns true if either of its operands is true. Otherwise, it returns false.
  • Not (!): Negates the value of its operand. If the operand is true, it returns false. Otherwise, it returns true.

Logical operators can be used to create more complex conditional statements. For example, the following code uses the && operator to check if a digit is in a specific range of numbers or not:

#include <iostream>

using namespace std;

int main()
{
    int x = 0;
    cout << "Enter a number";
    cin >> x;
    if (x >= 1 && x <= 100)
        cout << "Ok" << endl;
    else
        cout << "Out of range" << endl;

    return 0;
}

Another Example:

#include <iostream>

using namespace std;

int main()
{
    int a = 0;
    cout << "Enter your age";
    cin >> a;
    char g = '\0';
    cout << "Enter your gender";
    cin >> g;
    
    if (a < 18 && g == 'm')
        cout << "male, " << "young boy" << endl;
    else if (a >= 18 && g == 'm')
        cout << "male," << "grown up man" << endl;
    else if (a < 18 && g == 'f')
        cout << "female," << "young girl" << endl;
    else
        cout << "female," << "grown up girl" << endl;
    
    return 0;
}

Note : \0 in C++ is a null character. It is a character with all bits set to zero. It is used to mark the end of a string in C++.

The following code uses the || operator to check if a number is greater than 10 or less than 5:

#include <iostream>

using namespace std;

int main()
{
    int number = 3;

    if (number > 10 || number < 5) 
        cout << "The number is greater than 10 or less than 5." << endl;
    
    return 0;
}

The following code uses the ! operator to negate the value of the Boolean expression number > 10:

#include <iostream>

using namespace std;

int main()
{
    int number = 3;

    if (! (number > 10)) 
        cout << "The number is not greater than 10." << endl;
    
    return 0;
}

Logical operators can be used to create very complex conditional statements. However, it is important to use them carefully to avoid making your code difficult to read and maintain.

Here are some general rules for using logical operators:

  • Logical operators can be used to combine Boolean expressions to create more complex conditional statements.
  • The && operator returns true if both of its operands are true. Otherwise, it returns false.
  • The || operator returns true if either of its operands is true. Otherwise, it returns false.
  • The ! operator negates the value of its operand. If the operand is true, it returns false. Otherwise, it returns true.
  • Logical operators can be used in nested expressions.

By understanding how to use logical operators, you can write more efficient and reliable C++ code.

Share this

Logical Operators

Or copy link

CONTENTS
English