Page 347 - Computer_Science_F5
P. 347

Computer Science  <<=  Performs a bitwise XOR operation on the  a ^= 3; // a = a ^ 3
                ^=
                       variable on the left and the value on the
                       right and assigns the result to the variable
                       on the left.
          FOR ONLINE READING ONLY
                       Performs a left bitwise shift on the variable  a <<= 2; // a = a << 2
                       on the left by the number of positions
                       specified  on  the  right  and  assigns  the
                       result to the variable on the left.
               >>=     Performs a right bitwise shift on the  a >>= 1; // a = a >> 1
                       variable  on the left by the number of
                       positions specified on the right and assigns
                       the result to the variable on the left.
              >>>=     Performs an unsigned right bitwise shift  a >>>= 1; // a = a >>> 1
                       on the variable on the left by the number
                       of  positions  specified  on  the  right  and
                       assigns the result to the variable  on the
                       left.


           (f) Ternary operator
           The ternary operator in Java is a shorthand for an if-else statement. It is used to
           evaluate a boolean expression and return one of two values based on the result of
           the evaluation.

           Syntax
           In Java,  the  ternary  operator  (also  known as the  conditional  operator)  has the
           following syntax:

               variable = (condition) ? expression1 : expression2;

           In this syntax;

            condition:    Represent a boolean expression that evaluates to either true or false.

            expression 1:  Represents “If the condition is true, the variable is assigned the value
                          of expression1”.


            expression 2:  Represent “If the condition is false, the variable is assigned the value
                          of expression2”.


           The Program Example 5.10 demonstrates the ternary operators:

                                                 338
                                                                for Advanced Secondary Schools



     Computer Science Form 5.indd   338                                                     23/07/2024   12:34
   342   343   344   345   346   347   348   349   350   351   352