C++

OR Operator in C++

One of the fundamental building blocks in C++ programming is the logical operators. Among these, the logical OR operator controls the program flow and decides based on conditions. In this article, we will delve into the syntax and usage of the “or” operator and explore how it contributes to the development of conditional statements and logical expressions in C++ code.

Syntax:

The “or” operator in C++ is a fundamental logical operator that allows the developers to create the conditional expressions and make decisions based on multiple conditions. Also known as the “logical OR” operator, it is represented by the double vertical bar symbol “||”.

Because it is binary, the logical OR operator needs two operands in order to function. The syntax is as follows:

expression1 || expression2

Here, “expression1” and “expression2” are the two operands. If at least one of the expressions is true, the outcome of the OR operation is true; if not, it is assessed as false.

Example 1: Basic Usage of the OR Operator

Here’s a simple example to illustrate the basic usage of the “or” operator:

#include <iostream>
Using namespace std;

int main() {
    bool condition1 = true;
    bool condition2 = false;

    if (condition1 || condition2) {
        cout << "At least one condition is true." << endl;
    } else {
        cout << "Both conditions are false." << endl;
    }

    return 0;
}

In this C++ code snippet, we create a simple program to demonstrate it using the “or” operator (||) in a conditional statement. Two Boolean variables, “condition1” and “condition2”, are initialized with the “true” and “false” values. The program then employs the “or” operator within an “if” statement to evaluate whether at least one of the conditions is true. If either “condition1” or “condition2” is true, the program executes the corresponding block of code inside the “if” statement, displaying the “At least one condition is true” message using the “cout” statement. On the other hand, if both conditions are false, the program executes the code inside the “else” block, printing “Both conditions are false” to the console.

The output of the provided C++ code will be:

Example 2: Input Validation

Input validation is a crucial aspect of programming to ensure that the data provided by the users or external sources meets the expected criteria. In C++, input validation often involves checking whether the entered values adhere to specified conditions before proceeding with further execution. One common scenario is validating the user input for numeric values, ensuring that they fall within a desired range.

Consider a program that prompts the user to enter their age and validates whether the input is within a reasonable range. Let’s create a simple example:

#include <iostream>
using namespace std;

int main() {
    int age;

   cout << "Enter your age: ";
   cin >> age;

    if (age < 0 || age > 100) {
        cout << "Invalid age entered." << endl;
    } else {
        cout << "Valid age entered." << endl;
    }

    return 0;
}

In this example, we develop a simple age validation system to ensure that the user input falls within a reasonable range. Initially, we prompt the user to enter their age using the “cout” statement. Then, we utilize the “cin” statement to capture the entered age into the “age” variable. After that, we employ the “or” operator (||) within an “if” statement to check whether the entered age is either less than 0 or greater than 100. If this condition is true, indicating that the age is outside the expected lifespan, we output “Invalid age entered”. Otherwise, if the entered age is within the acceptable range, the “else” block executes and we output “Valid age entered”.

The obtained output of the program is:

This output signifies that the user successfully inputs the age 35 in which the program recognized as valid according to the specified criteria.

Example 3: Checking the Divisibility

The “or” operator (||) can be employed to create conditions that test multiple divisibility criteria within a single logical statement.

Let’s consider a C++ program that checks if a given number is divisible by either 3 or 5:

#include <iostream>
using namespace std;

int main() {
    int number;


    cout << "Enter a number: ";
    cin >> number;


    if (number % 3 == 0 || number % 5 == 0) {
        cout << "The entered number is divisible by 3 or 5." <<endl;
    } else {
        cout << "The entered number is not divisible by 3 or 5." << endl;
    }

    return 0;
}

Here, we request the user input with a number through the console. The entered value is then stored in the variable number using the “cin” statement. Subsequently, we utilize the “or” operator (||) in the “if” statement to create a logical condition of “number % 3 == 0 || number % 5 == 0”. This condition determines whether the entered integer is divisible by 3 or 5 by dividing it by the remainder. If the condition is true, indicating the divisibility, we output the “The entered number is divisible by 3 or 5” message using the “cout” statement. If, on the other hand, the condition is false, we print “The entered number is not divisible by 3 or 5”.

While executing this program, we provide the value of 22.

The program checks whether the entered number (22) satisfies the specified condition. In this case, both number % 3 and number % 5 are non-zero, indicating that 22 is not divisible by either 3 or 5. Therefore, the “else” block is executed and the program outputs “The entered number is not divisible by 3 or 5”.

Example 4: Complex Conditions

This example illustrates the use of logical operators, specifically the “and” operator (&&) and the “or” operator (||), to make decisions based on the positions of points in a Cartesian coordinate system. We will address whether two points, represented by variables “x” and “y”, fall within the same quadrant.

Here is the code:

#include <iostream>
using namespace std;

int main() {
    int x = 5;
    int y = 10;

 
    if ((x > 0 && y > 0) || (x < 0 && y < 0)) {
        cout << "The points are in the same quadrant." << endl;
    } else {
       cout << "The points are in different quadrants." << endl;
    }

    return 0;
}

We initialize two integer variables, “x” and “y”, with the values of 5 and 10, respectively. The conditional statement inside the “if” block checks whether both “x” and “y” are greater than 0 (indicating the first quadrant) or both are less than 0 (indicating the third quadrant). If either of these conditions is true, the program outputs “The points are in the same quadrant” using “cout”. Conversely, if the conditions are not met, the program outputs “The points are in different quadrants” using the else block.

Upon execution, the code generates the following output:

Conclusion

In this comprehensive guide, we explored the “or” operator in C++, a fundamental tool for making logical decisions in programming. The article began by understanding the basic syntax of the “or” operator. Then, we delved into practical examples that showcased its versatility, ranging from primary usage and input validation to more complex scenarios such as checking the divisibility and handling multiple conditions.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content