Page 59 - Computer Genius Class 07
P. 59

Logical Operators

                 Logical operators are the fundamental blocks used to implement a decision-making capability in
                 the code. When you need to make decision based on two or more checks, it is necessary to have a
                 combination of logical operators.

                 Logical operators work like Boolean variables and return either TRUE or FALSE.
                 The three most important logical operators are:

                 AND Operator

                     It is used to see if two or more conditions are true.
                     If all the conditions are true, the AND operator returns TRUE.
                     If any one of the conditions fail, the AND operator returns FALSE.

                     In python AND operator is denoted by ‘and’ keyword.
                     Some other programming languages use ‘&&’ as AND operator.

                 OR Operator

                     It is used to see if either one of two or more conditions are TRUE.
                     If any of the condition is true, the OR operator returns TRUE.
                     If all the conditions false, the OR operator returns FALSE.

                     In python OR operator is denoted by ‘or’ keyword.
                     Some other programming languages use ‘||’ as OR operator.

                 NOT Operator
                     It is used to reverse or negate a condition.

                     If the condition is true, NOT will return false and vice-versa.
                     In python NOT operator is denoted by ‘not’ keyword.

                     Some other programming languages use ‘!’ as NOT operator.
                 Relational Operators

                 The relational operator checks the value of the two operands.

                 Relational operator returns:
                     1, if the value is true.
                     0, if the value is false.

                 Types of relational operators:


                   Symbol              Operator                            Description                   Example


                      ==      Equal to                      a is equal to b                               (a==b)

                      !=      Not equal to                  a is not equal to b                           (a !=b)

                      >       Greater than                  a is greater than b                           (a > b)


                                                                                Advanced MakeCode Arcade      57
   54   55   56   57   58   59   60   61   62   63   64