Switch Statement#
Key Ideas#
Switch Statement
Readings#
Lecture Code#
Definition
A switch statement acts like an if statement, but there are definite choices. The switch statement will allow specific code to run pending the choice. Swtich statements are usually used in building menus, but can be used anywhere where there is a definite choice.
Example#
/* Switch / Menu Demo
This is a demo program to demonstrate a design pattern for creating
a menu system in the console environment.
:)---- Bob Ross Coding ---- :)
Jim Goudy
*/
import java.util.Scanner;
public class Menu_Demo {
// Happy Function 1
static void HappyFunction1() {
System.out.println("\n\nThis is happy function 1");
}
// Happy Function 2
static void HappyFunction2() {
System.out.println("\n\nThis is happy function 2");
}
// Happy Function 3
static void HappyFunction3() {
System.out.println("\n\nThis is happy function 3");
}
// This function creates a menu system.
static void menu() {
// variables
int choice = 0; // hold user choice
// Scanner for user input
Scanner myScan = new Scanner(System.in);
// display the user choices
System.out.println("\nMENU\n"
+ "\n1. Happy Function 1"
+ "\n2. Happy Function 2"
+ "\n3. Happy Function 3"
+ "\nPlease choose a function 1,2 or 3");
choice = Integer.parseInt(myScan.nextLine());
// take the choice stored in choice and use it in the switch
// case values can be an int, char or enum
switch (choice) {
case 1:
HappyFunction1();
break;
case 2:
HappyFunction2();
break;
case 3:
HappyFunction3();
break;
default:
// This is optional
// default is used if the user entered any
// number that was note 1,2 or 3
System.out.println("\nThat wasn't a choice");
}
}
public static void main(String[] args) {
// variables
String quit = "n";
// Scanner for user input
Scanner myScan = new Scanner(System.in);
// quit has to be initialized with the value of n
// so the loop will run at least once. Putting the menu
// call in a while statement will allow the user to run
// the program as many times as they wish.
while (quit.equals("n")) {
// call the menu
menu();
// prompt the user if they want to quit
System.out.println("\nWould you like to quit - y or n");
quit = myScan.nextLine().toLowerCase();
}
// Inform the user the program is done executing
System.out.println("Good Bye");
}
}
New format of switch statement after version 14+#
/*
* Switch Statement
* As of Java 14 went to the "->"
*/
package com.mycompany.switch_statement_revised;
import java.util.Scanner;
/**
*
* @author jgoudy
*/
public class Switch_statement_revised {
static int menu() {
int choice = 0;
try {
Scanner myScan = new Scanner(System.in);
System.out.print("Menu\n"
+ "1. Pick Larry\n"
+ "2. Pick Curly\n"
+ "3. Pick Moe\n"
+ "Please pick 1,2 or 3: ");
choice = Integer.parseInt(myScan.nextLine());
return choice;
} catch (Exception e) {
return -1;
}
}
static void Larry() {
System.out.println("You chose Larry");
}
static void Curly() {
System.out.println("You chose Curly");
}
static void Moe() {
System.out.println("You chose Moe");
}
static void switch_example1() {
// this format is the same in most languages
// and in all version of java
int choice = -1;
System.out.println("Example 1");
choice = menu();
switch (choice) {
case 1:
Larry();
break;
case 2:
Curly();
break;
case 3:
Moe();
break;
default:
System.out.println("That wasn't a choice");
}
System.out.println("\n--------------------------\n");
}
static void switch_example2() {
// This format of a switch effective version 14+
int choice = -1;
System.out.println("Example 1");
choice = menu();
switch (choice) {
case 1 -> Larry();
case 2 -> Curly();
case 3 -> Moe();
default ->
System.out.println("That wasn't a choice");
}
System.out.println("\n--------------------------\n");
}
public static void main(String[] args) {
switch_example1();
switch_example2();
}
}
End Of Topic