Page 190 - PowerPoint Presentation
P. 190

CAVITE STATE UNIVERSITY
                               T3 CAMPUS
                               Department of Information Technology          COSC 65 – Programming Languages

               Java Switch Statement
                       The Java switch statement executes one statement from multiple conditions. It is like
               if-else-if ladder statement. The switch statement works with any data type like int, char, string
               and more.
                       In other words, the switch statement tests the equality of a variable against multiple
               values.

                  //Syntax

                  switch (expression) {
                  case value1:
                        // code to be executed
                  break;                                public class SwitchExample {
                  case value2:                                 public static void main(String[] args) {
                        // code to be executed
                  break;                                int num=20;
                  ..                                    char a= ‘A’;
                  default:                              string h= “Hello”;
                  // code to execut if all cases are not
                  matched                               switch (num) {  // can be change by a or h.
                  }                                     case 10:
                                                               System.out.println(“The number is 10”);
                                                        break;
                  NOTE:
                  If char is used as the data type in   case 20:
                  switch statement, we should add              System.out.println(“The number is 20”);
                  single apostrophe (‘   ‘ ) .          break;

                  Example:                              case 30:
                        switch (variable_name):                System.out.println(“The number is 30”);
                               case ‘A’ or ‘+’ :        break;
                                       System………
                               break;
                                                        default:
                  If string is used as the data type in        System.out.println(“Invalid”);
                  switch statement, we should add       break;
                  double apostrophe (“ “).              }
                                                        }
                  Example:                              }

                        switch(variable_name):
                               case “String”:
                                       System…..
                               break;

               Discussion: On you own words, what is the importance of using conditional statements in a
               program.
               _________________________________________________________________________
               _________________________________________________________________________
               _________________________________________________________________________
               _________________________________________________________________________
               _________________________________________________________________________
               _________________________________________________________________________
               _________________________________________________________________________
               _________________________________________________________________________


                                                                                                 Page | 49
   185   186   187   188   189   190   191   192   193   194   195