For example, if the value of numDay variable is 1, the output will be Monday and for the value of 7, it should be Sunday. After that, seven case statements are used one for each day of the Week. A variable is assigned a value (number of the day in Week) which is used as an expression in the switch statement. In this example, the switch statement is used with seven cases. } An example of using switch case and default statements Java statement here if case 2 is evaluated as trueĭefault: // It will execute if none of the case is true Java statement here if case 1 is evaluated as trueīreak // It will terminate the switch statement The general way of using the switch case is: Structure of switch case statement in Java After that, I will explain the difference between the if and switch statement and which one to use depending on the scenario. See the following section for structure and examples of using the switch case statement. The break statement is necessary to exit the switch statement (see explanation in the first example).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |