Page 38 - CTS - CSA TP - Volume 2
P. 38

COMPUTER SOFTWARE APPLICATION - CITS




           Explanation:
           1  Ternary Conditional Operator (? :):

              •  The ternary conditional operator is a shorthand way of writing an if-else statement.
              •  The general form is condition ? expression1 : expression2.
              •  If the condition is true, it evaluates to expression1; otherwise, it evaluates to expression2.
           2  Program Logic:
              •  The program defines two integer variables a and b with values 2 and 5, respectively.

              •  It uses the ternary conditional operator to find the minimum of a and b.
              •  The condition (a < b) is evaluated. If true, a is assigned to min; otherwise, b is assigned to min.
              •  The minimum value is then printed to the console.
           3  Example:
              •  int min = (a < b) ? a : b;

              •  The condition (a < b) is true (2 is less than 5).
              •  Therefore, min is assigned the value of a (2).
              •  Output: 2
           Output:















           TASK 12: Java Assignment Operator Example

           public class OperatorExample12{
           public static void main(String args[]){
           int a=10;
           int b=20;
           a+=4;//a=a+4 (a=10+4)

           b-=4;//b=b-4 (b=20-4)
           System.out.println(a);
           System.out.println(b);
           }}
           Output:















                                                           23
 CITS : IT & ITES - Computer Software Application - Exercise 82  CITS : IT & ITES - Computer Software Application - Exercise 82
   33   34   35   36   37   38   39   40   41   42   43