Switch Case Java

The switch case in java matches the given value from multiple values and executes the corresponding switch statement. Java Switch contains multiple cases. In each switch case, there is a code block.The Java switch case expression is used to evaluate a variable with several values. The Switch case supports Enum, String, primitive data types, and Wrapper data types.

The Switch case statement in java is a type of conditional statement. Switch statement string contains multiple cases. Each case switch string has a block of code. Switch Statement will execute the code block for the matched condition. Unlike if statement, the Switch statements checks the equality of the matched values. It is preferable to use switch statements for discrete values.

The java break statement is used to exit the sequence of the statement in the switch. The execution will proceed in the next case, if the break statement is removed. The default statement is optional and can be added anywhere within the switch block. If the switch matches the default and no break statement added at the end of default case, the execution will continue to the next case statements.



Datatypes supported by Switch Statements

The following datatypes are provided in the execution of the java switch case statement. The data type of all case values and the switch value must be the same.

  • Primitive datatypes – byte, short, int, long
  • Wrapper datatypes classes – Byte, Short, Integer, Long
  • String Class
  • Enum types


Switch Statements

Below is the syntax for the Java Switch statement. The Switch statement contains multiple conditions. If there is no matching condition, the default block will be executed. Each code block should end with a break statement. Otherwise, the next code block will be executed.

Syntax

switch (value) {
case data1:
   block of code 1 ...
   break;
case data2:
   block of code 2 ...
   break;
.........
.........
case data-n:
   block of code n ...
   break;
default :
   block of code for default ...
}

Example

package com.yawintutor;

public class SwitchWithBreak {
	public static void main(String[] args) {
		int day = 4;

		System.out.print("Day of the week for the value " + day + " is ");

		switch (day) {
		case 1:
			System.out.println("Sunday");
			break;
		case 2:
			System.out.println("Monday");
			break;
		case 3:
			System.out.println("Tuesday");
			break;
		case 4:
			System.out.println("Wednesday");
			break;
		case 5:
			System.out.println("Thursday");
			break;
		case 6:
			System.out.println("Friday");
			break;
		case 7:
			System.out.println("Saturday");
			break;
		default:
			System.out.println("Invalid value entered");
			break;
		}
	}
}

Output

Day of the week for the value 4 is Wednesday


Switch Case Statements without break

Switch statement executes code blocks followed by a matched block. If there is no break statement at the end of the code block, it will continue to the next code block and so on. Below example shows how Switch statement works without break statement.

package com.yawintutor;

public class SwitchWithoutBreak {
	public static void main(String[] args) {
		int day = 4;

		System.out.print("Day of the week for the value " + day + " is ");

		switch (day) {
		case 1:
			System.out.println("Sunday");

		case 2:
			System.out.println("Monday");

		case 3:
			System.out.println("Tuesday");

		case 4:
			System.out.println("Wednesday");

		case 5:
			System.out.println("Thursday");

		case 6:
			System.out.println("Friday");

		case 7:
			System.out.println("Saturday");

		default:
			System.out.println("Invalid value entered");

		}
	}
}

Output

Day of the week for the value 4 is Wednesday
Thursday
Friday
Saturday
Invalid value entered


Switch Case Statements with Wrapper Classes

The following example shows how to use Wrapper classes in a Java Switch Statements.

package com.yawintutor;

public class SwitchWithWrapper {
	public static void main(String[] args) {
		Integer day = new Integer(4);

		System.out.print("Day of the week for the value " + day + " is ");

		switch (day) {
		case 1:
			System.out.println("Sunday");
			break;
		case 2:
			System.out.println("Monday");
			break;
		case 3:
			System.out.println("Tuesday");
			break;
		case 4:
			System.out.println("Wednesday");
			break;
		case 5:
			System.out.println("Thursday");
			break;
		case 6:
			System.out.println("Friday");
			break;
		case 7:
			System.out.println("Saturday");
			break;
		default:
			System.out.println("Invalid value entered");
			break;
		}
	}
}

Output

Day of the week for the value 4 is Wednesday


Switch Case Statements with String

The following example shows how to use String classes in a Java Switch Statements.

package com.yawintutor;

public class SwitchWithString {
	public static void main(String[] args) {
		String day = "Wednesday";

		System.out.print("Day of the week for the value " + day + " is ");

		switch (day) {
		case "Sunday":
			System.out.println("Sunday");
			break;
		case "Monday":
			System.out.println("Monday");
			break;
		case "Tuesday":
			System.out.println("Tuesday");
			break;
		case "Wednesday":
			System.out.println("Wednesday");
			break;
		case "Thursday":
			System.out.println("Thursday");
			break;
		case "Friday":
			System.out.println("Friday");
			break;
		case "Saturday":
			System.out.println("Saturday");
			break;
		default:
			System.out.println("Invalid value entered");
			break;
		}
	}
}

Output

Day of the week for the value Wednesday is Wednesday


Switch Case Statements with enum types

The following example shows how to use enum type in a Java Switch Statements.

package com.yawintutor;

public class SwitchWithEnum {
	enum Color {
		Blue, Green, Red, Yellow
	};

	public static void main(String[] args) {
		Color color = Color.Red;
		int price = 0;

		System.out.println("T-shirt color : " + color);

		switch (color) {
		case Blue:
			price = 100;
			break;
		case Green:
			price = 200;
			break;
		case Red:
			price = 300;
			break;
		case Yellow:
			price = 400;
			break;
		default:
			System.out.println("Invalid value entered");
			break;
		}

		System.out.println();
		System.out.println("T-shirt price : " + price);
	}
}

Output

T-shirt color : Red

T-shirt price : 300


Nested Switch Case Statements

The following example shows how to use Java Switch Statement with in an another Java Switch Statements.

package com.yawintutor;

public class SwitchWithNested {
	public static void main(String[] args) {
		String color = "Red";
		String model = "XL";
		int price = 0;

		System.out.println("T-shirt color : " + color);
		System.out.println("T-shirt model : " + model);
		switch (model) {
		case "X":
			switch (color) {
			case "Red":
				price = 100;
				break;
			case "Blue":
				price = 200;
				break;
			default:
				price = 150;
				break;
			}
			break;
		case "XL":
			switch (color) {
			case "Red":
				price = 200;
				break;
			case "Blue":
				price = 300;
				break;
			default:
				price = 250;
				break;
			}
			break;
		case "M":
			switch (color) {
			case "Red":
				price = 300;
				break;
			case "Blue":
				price = 500;
				break;
			default:
				price = 350;
				break;
			}
			break;
		default:
			System.out.println("Invalid Model");
			break;
		}

		System.out.println();
		System.out.println("T-shirt price : " + price);

	}
}

Output

T-shirt color : Red
T-shirt model : XL

T-shirt price : 200



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *