Close
Close

I am unable to understand the operators like && or ||

   Sharonya Banerjee

I am unable to understand && and || I need an answer 


Answers

  •   

    && is a logical AND operator.  You can think of it as similar to an AND gate in circuity.  || is a logical OR operator.  You can think of it as similar to an OR gate in circuitry.

    For example:

    if((5 + 10 == 15) && (5 - 10 == 15)):
         printf("Both 5 + 10 is equal to 15, AND 5 - 10 is equal to 15.");
    

    In this code, the expression will evaluate to True if 5 + 10 is 15, AND if 5 – 10 is 15.  Because 5 - 10 is not 15, the expression will evaluate to False, and the printf() call will not execute.

    if((5 + 10 == 15) || (5 - 10 == 15)):
         printf("Either 5 + 10 is equal to 15, OR 5 - 10 is equal to 15.");
    

    In this code, the expression will evaluate to True if 5 + 10 is 15, OR if 5 – 10 is 15.  Because 5 + 10 is 15, the expression will evaluate to True, and the printf() call will execute.

    edit:  corrected typos in the printf string


    • Thank you for your time
      - Sharonya Banerjee

Ask Yours
Post Yours
Write your answer