Page 149 - Cs_withBlue_J_C11_Flipbook
P. 149

The output of the preceding program is given below:
                 If the value of m is 20 and n is 5
                 == operator : false
                 != operator : true
                 > operator : true
                 < operator : false
                 >= operator : true
                 <= operator : false

                 Logical Operators
                 A logical operator is used to check whether an expression results in true or false. There are three types of logical
                 operators. They are as follows:

                        Operator       Meaning                 Description               Example           Result
                           &&            AND         Returns True if both the conditions are  (5>6) && (1<5)  False
                                                     satisfied else will return false.
                           ||             OR         Returns False if both the conditions do  (11==11) || (3>20)  True
                                                     not satisfy else will return true.
                            !             NOT        Reverses the result.            ! (4==4)         False

                 Some Solved Examples:
                   1.  3>2
                 Ans.  True                   Reason: 3>2 is true
                   2.  (4+2)>5
                 Ans.  True                   Reason: (4+2)>5 is true
                   3.  3>7 || 8<12
                 Ans.  True                   Reason: 8<12 is true

                   4.  (3==5) && (2!=4)
                 Ans.  False                  Reason: 3==5 is false
                   5.  2==0 && 5<(3+4)
                 Ans.  False                  Reason: 2==0 is false

                   6.  5!=5 || (1+2)==3
                 Ans.  True                   Reason: 3==3 is true
                   7.  !(8==8)
                 Ans.  false                  Reason: 8==8 is true and not of true is false
                   8.  !(4>12)
                 Ans.  True                   Reason: 4>12 is false and not of false is true
                   9.  (4!=2) || (4>4)
                 Ans.  True                   Reason: 4 !=2 is true


                 Conditional Assignment Operator
                 This operator consists of three operands, so it is also known as the ternary operator. The three operands may be
                 variables, expressions or constant values. The syntax of the ternary operator is as follows:
                 variable = (test condition)? Expression1 : Expression2;




                                                                                                                       147
                                                                                               Variables and Expressions  147
   144   145   146   147   148   149   150   151   152   153   154