c++if-statementlogicmultiple-conditions

Multiple Conditions in if statement in both "OR" and "AND" operation in C++


I'm a bit confused with "or" and "and" operations inside "if" statements. Here, if(condition_1 || condition_2) in this operation, when the OR operator checks both of the conditions and its behavior of checking them. Also if(condition_1 && condition_2) in this operation, when the && operator checks both of the conditions and its behavior of checking the conditions.

How "OR" and "AND" operation works in checking two or multiple conditions?


Solution

  • The "OR" operator will return true when one or both of the two conditions is true, and will short circuit if the first item is true.

    if (condition_1 == true || condition_2 == false) {
       //code will run if either or both of the conditions is true
    }
    

    The "AND" operator will only work when the both conditions are true and will short circuit if the first operand is false.

    if (condition_1 == true && condition_2 == true) {
       // code will run when both conditions are true
    }