• The Java Switch Statement

    We already learned how to use If, Else If, Else statements as well as For, While and Do-While loops. The java switch statement function the same way just like series of if statements. Switch statements are used if comparing a value to limited number of options. Consider the example below:

    String findName = "John";
    
    switch(findName){
         case "Mary":
              System.out.println("Your name is Mary!");
              break;
         case "Anne":
              System.out.println("Your name is Anne!");
              break;
         case "John":
              System.out.println("Your name is John!");
              break;
         case "Michael":
              System.out.println("Your name is Michael!");
              break;
    }
    

    In our example, we have a string variable with value John. The switch statement requires a value that will be compared. In our case, we have switched findName variable that is also equal to John. The switch statement will then search the case that matches the variable findName. Since case “Mary” is not the same in our variable, it will be skip and the inline codes will not be executed. The same will go for case “Anne”. When the switch statement found case “John”, it knows that our variable findName is equal to John, therefore it will go inside the case and perform the statements or codes inside that case. It will then print “Your name is John!” in the console. Next, it will execute the break statement. Remember that break statement will cause the loop to terminate. Therefore it will not go further to case “Michael” and will just end the switch statement.

    Switch statement without break

    Switch statements without break will cause to execute other lines even if it doesn’t match the condition. Consider the example below:

    String findName = "John";
    
    switch(findName){
         case "Mary":
              System.out.println("Your name is Mary!"); 
         case "Anne":
              System.out.println("Your name is Anne!");          
         case "John":
              System.out.println("Your name is John!");          
         case "Michael":
              System.out.println("Your name is Michael!");         
    }
    

    The above example will cause the program to execute the case John and case Michael. The output will be:

    Your name is John!
    Your name is Michael!
    

    Using default keyword

    Switch statement with default is just like series of if – else if – else statement. If the switch statement doesn’t have any case that satisfies the condition, it will then execute the default case. For example:

    String findName = "Jane";
    
    switch(findName){
         case "Mary":
              System.out.println("Your name is Mary!");
              break;
         case "Anne":
              System.out.println("Your name is Anne!");
              break;
         case "John":
              System.out.println("Your name is John!");
              break;
         case "Michael":
              System.out.println("Your name is Michael!");
              break;
         default:
              System.out.println("Your name is not in our list.");
              break;
    }
    

    Here, the switch statement need to find the case which is equal to Jane. Since there is no case that satisfies the condition, it will then fall to the default case. Thus, it will print “Your name is not in our list.” in the console. The default case will only be called when there is no case that matches the variable that is to be switched.

    Try to create your own java switch statement to familiarize yourself. Next on our tutorial is how to create constructors in Java.

    Related Post