Page 163 - ComputerScience_Class_11
P. 163

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;
                 The operator evaluates the test condition first and then the expression based on the result of the condition. If the
                 condition results in true, expression1 will be evaluated and if the condition returns false, expression2 will be evaluated.
                 Some solved examples:
                   1.  int a = 5, b = 6, large;
                       large = (a > b)? a : b;
                 Ans.  6
                   2.  double d1 = 10.5, d2 = 5.5, small;
                       small = (d1 + d2 > 0)? d1 : d2;
                 Ans.  10.5
                   3.  int c = (3 < 4)? 3 * 4 : 3 + 4;
                 Ans.  12




                                                                                            Variables and Expressions  161
   158   159   160   161   162   163   164   165   166   167   168