Java - Control Flow Statements - The Switch Statement

This article continues on Control Flow Statements. We take a look at the Switch Statement with code examples.

The Switch Statement

The switch statement like, "if/then/else" is used to test for conditions and execute code. However unlike "if/then/else", a "switch" can execute more than one code block. A switch is given a value which is tested against a case label. If the value evaluates to true then the code block for that case label will be executed. Case labels can be of several data types namely byte, short, char, and int primitive data types as well as strings, enums and a few other special classes.

A switch statement also has a "default" case label which executes a code block when no other case label  matches the condition. The code below demonstrates a simple "switch" statement.

  1. class Program {   
  2.     public static void main(String args[]){   
  3.        
  4.         int userType = 2;   
  5.            
  6.         switch (userType) {   
  7.             case 1:   
  8.                 System.out.println("Hello admin");   
  9.                 break;   
  10.             case 2:   
  11.                 System.out.println("Hello supper admin");   
  12.                 break;   
  13.             default:   
  14.                 System.out.println("Who are you?");   
  15.                    
  16.         }   
  17.     }   
  18. }  
In the above code sample the variable userType, is of type int, the "switch" statement excepts this variable and it's case labels determine if there is a match. When a match is found a message is displayed and execution of the "switch" statement is terminated because of the break keyword and the code resumes it's normal execution flow. When no match is found, the default case label is executed.

If a case label does not have a break statement, the "switch" statement will continue to execute label blocks regardless of the label expression until it either encounters a break statement or the "switch" statement has completed is execution flow.

The following code sample prints the number 3-6.

  1. class Program {   
  2.     public static void main(String args[]){   
  3.        
  4.         int index = 3;   
  5.            
  6.         switch (index){   
  7.             case 1:   
  8.                 System.out.println("1");   
  9.             case 2:   
  10.                 System.out.println("2");   
  11.             case 3:   
  12.                 System.out.println("3");   
  13.             case 4:   
  14.                 System.out.println("4");   
  15.             case 5:   
  16.                 System.out.println("5");   
  17.             case 6:   
  18.                 System.out.println("6");   
  19.         }   
  20.     }   
  21. }  
In the above code sample, the case label "3" matches the expression, however since the case blocks do not have a break statement, each label block after the match is executed. This results in the numbers 3-6 being printed.

No comments:

Post a Comment