Recommended Books

Java Switch Statement



This article is about the use of the switch statement in Java.

The Java Switch statement is one of the selection type statements in Java, similar to the if else statement. It creates multiple paths with unique outcomes, allowing a program to pick and traverse one of these paths.

The path to be taken is determined by the value of a given expression.

Syntax

Each value specified in the case statements must be a unique expression. Duplicates cases are not allowed. There is no limit to the number of cases allowed.

switch(expression) {
      case constant1:
          //instructions to be executed
          break;
      case constant2:
          //instructions to be executed
          break; 
      ......
      ......
      default:
          //instructions to be executed
}

In the place of expression we write the expression which will produce a value which will control the switch path taken. The final of the expression must always be either of type char, byte, int, short or an enumeration.

In the place of the constants in front of case we place values to which the value of the expression will compared against.

Finally, we have the default statement. The code block belonging to the default statement will trigger in the event that there was no match for any of the previous case values. It is way of ensuring there is some output, regardless of whether a match was found or not.


Example

Here’s a working example of the switch statement and how it’s used in Java. This example is not a very practical one, but it’s suitable for learning purposes due to it’s simplicity.

public class example {
	public static void main(String[] args) {
		int a = 4;
		
		switch (a) {
		case 0:
			System.out.println("a is 0");
			break;
		case 1:
			System.out.println("a is 1");
			break;
		case 2:
			System.out.println("a is 2");
			break;
		case 3:
			System.out.println("a is 3");
			break;
		case 4:
			System.out.println("a is 4");
			break;
		case 5:
			System.out.println("a is 5");
			break;
		default:
			System.out.println("a is greater than 5");
		}				
	}
}

The purpose of the above code is to check the value of the expression a, compare it with the existing cases and pick the path of the matching value. The above code will have the output "a is 4".

The break used in each code block are not actually part of the syntax for the switch statement. It’s just common to use them. Normally. if a condition matches, the program will continue to the other cases anyway and attempt to find a match. Using the break statement will stop the program as soon as a match is found, increasing efficiency.

Nested Switch statements

Similar to how nested if statements are, you can create nested switch statements too. There is no special syntax. It is merely a switch statement within a switch statement. This can be used to achieve greater functionality and create complex programs.


This marks the end of the Java Switch Statement page. If you have any suggestions or contributions for CodersLegacy, please share them. Any questions can be asked in the comments below.

Leave a Reply

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