Page 193 - ComputerScience_Class_11
P. 193

The syntax of the ternary operator is:

                 condition ? expressionIfTrue : expressionIfFalse;
                 We can represent nested conditional statements as ternary operators (already discussed in the previous chapter). The
                 above example can be written using Ternary Operator as:
                                        max = (a>b)? ((a>c)? a: c) : ((b>c)? b : c);

                 Switch Case
                 Switch Case is a multiple-branch selection statement in which multiple conditions can be checked at once. It uses a
                 variable that selects one of many codes (cases) to be executed.
                 The value of the variable or the expression is checked with the value of each case. When a match is found, the associated
                 block of code is executed. If no case is matched, the code of the default case will be executed which is provided at the
                 end of the switch statement.
                        Note:  The data type of the expression or the variable provided in the switch statement must match with
                        the data type of the case value or expression.

                 Syntax:

                    switch(choice)
                    {
                        case value1: sequence of statement1;
                            break;
                        case value2: sequence of statement2;
                            break;
                        case …..
                        .
                        .
                        default: statements;
                    }
                 Flowchart:
                                                Switch
                                         Conditional expression

                                                              True
                                                Case                    Statement 1
                                              Condition 1                  break;

                                         False
                                                              True
                                                Case                    Statement 2
                                              Condition 2                  break;

                                          False
                                                  
                                                  
                                                  
                                          False
                                                              True
                                                Case                    Statement n
                                              Condition n                  break;
                                          False
                                                              True
                                               Default                    Default
                                                                         Statement
                                                                                    Statement just
                                                                                   below switch case




                                                                                              Statements and Scope  191
   188   189   190   191   192   193   194   195   196   197   198