Page 175 - CA_Blue( J )_Class9
P. 175

You will get the following output: 1

















                 In the above output, when we give 3 as input, the control executes case 3, “Day number 3 so its Wednesday.”
                 and moves on to the next statement. The next statement is ‘break’ which halts the execution of the switch
                 statement.

                 You will get the following output: 2
















                 In the above output, when we give 1 as input, the control checks the first case and executes the first statement.
                 The control then moves to the next statement and executes the second and moves on to the next case. This is
                 continued till the control encounters a break statement. You may notice that all the statements are executed one
                 by one. This scenario is called the fall through condition. In other words, it is a special condition when the control
                 executes all the cases in a switch statement until it encounters a break statement.

                 You will get the following output: 3

















                 In the above output, when we give 8 as input, it is compared against all the cases. Since none of the cases match,
                 the default case is executed.



                       Note:  The default case is optional.  This statement is used to execute a task when none of the cases
                       match the input. Since it is the last case of Switch statement, ‘break’ is not needed.





                                                                                     Conditional Constructs in Java  173
   170   171   172   173   174   175   176   177   178   179   180