Day 8 : C++Language| Logical Operators

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MyrinNew
    Senior Member
    • Feb 2024
    • 5168

    #1

    Day 8 : C++Language| Logical Operators

    Logical Operators

    The three most commonly used logical operators are AND, OR, and NOT


    AND (&&)

    This operator requires that all connected conditions be true for the overall statement to be true.


    For instance, in programming, if A and B represent conditions, A AND B returns true only if both A and B are true.






    #include
    using namespace std;

    int main() {
    int x = 6;
    int y = 4;

    cout 3 && x
    // returns true (1) because 6 is greater than 3 AND 4 is less than 10 .

    //since both statement are true , result will be true satisfying and operator

    return 0;
    }







    OR ( || )

    This operator allows for flexibility—only one of the connected conditions must be true.


    In the statement A OR B, the result will be true if either A or B (or both) is true.






    #include
    using namespace std;

    int main() {
    int x = 5;
    int y = 3;
    cout 1 || x
    // returns true (1) because one of the conditions are true
    (5 is greater than 1, but 5 is not less than 3)

    return 0;

    }







    NOT (!)

    NOT inverts the truth value of a single condition.

    If A is true, then NOT A will be false, and vice versa.






    #include
    using namespace std;

    int main() {
    int x = 6;
    int y = 4;

    cout 3 && x // returns false (0) because ! (not) is used to reverse the result

    return 0;
    }







    Uses of logical operators

    They are implemented in conditional statements, loops, and algorithms to enable computers to make decisions.

    For example, imagine an e-commerce platform where discounts are applied only if a customer is both a loyalty member AND has spent over a certain amount.





    Logical operators will be explained in detail while learning loops

    Previous Blogs





    More...
Working...