Java Using Switch Statements

Key Points:

  • Switch Statement: is a type of decision mechanism used to allow the value of a variable or expression to change the control flow of program execution through a multiway branch. 
  • Example:
    int x = 1;
    switch (x){
    case 1:
    //do this
    break;
    case 2:
    //do this
    break;
    default:
    //if all else then do this…
    }
  • At the end of each case you must have a break statement before beginning the next case.
  • You must also have a default case, so if the variable value does not match any of the cases, it will execute default. 

View The Code!