Java Switch Statement with Example

A programming language uses control statements to control the flow of execution of a program. In Java programming, we can control the flow of execution of a program based on some conditions. Java control statements can be put into the following three categories: selection, iteration, and jump.


Java Switch Statement:

The Switch Statement can have a number of possible execution paths. It can be used to execute one statement from multiple conditions. A switch statement can work with byte, short, char, and int primitive data types. It also works with enumerated types and the String class. The Switch statement works like this:

The value of the expression is compared with each of the literal values in the case statements. If a match is found, the code sequence following that case statement is executed. If none of the constants matches the value of the expression, then the default statement is executed. The default statement is optional. If no case matches and no default is present, then no further action is taken.

The break statement is used inside the switch to terminate a statement sequence. When a break is encountered, execution branches to the first line of the code that follows the entire switch statement.



Syntax of Switch Statement:


switch(expression){
 case value1:
  //statement sequence
 break;
 case value2:
  //statement sequence
 break;
.......
.......
.......
 case valueN:
  //statement sequence
 break;
 default:
  //default statement sequence
}



Flowchart of Java switch statement:

Java Switch Statement



Java Switch Statement Example:

class SwitchStatement{
	public static void main(String args[]){
		for(int i = 0; i<6; i++)
			switch(i){
				case 0:
				System.out.println("i is zero.");
				break;
				case 1:
				System.out.println("i is one.");
				break;
				case 2:
				System.out.println("i is two.");
				break;
				case 3:
				System.out.println("i is three.");
				break;
				default:
				System.out.println("i is greater than 3.");
				
			}
	}
}



Output:
Java Switch Statement

As you can see, each time through the loop, the statements associated with the case constant that matches i are executed. All others are bypassed. After i is greater than 3, no case statement match, so the default statement is executed.



The break statement is optional. If you omit the break, execution will continue on into the next case. It is sometimes desirable to have multiple cases without break statements between them.



Java Switch Case Example without break:


class SwitchStatement{
	public static void main(String args[]){
		for(int i = 0; i<12; i++)
			switch(i){
				case 0:
				case 1:
				case 2:
				case 3:
				case 4:
				System.out.println("i is less than 5.");
				break;
				case 5:
				case 6:
				case 7:
				case 8:
				case 9:
				System.out.println("i is less than 10.");
				break;
				default:
				System.out.println("i is 10 0r more.");
				
			}
	}
}


Output:
Java Switch Statement


You may also like these related posts:

1. Java if-else-if ladder with example.
2. Java nested-if statement with example.
3. Java if-else statement with example.
4. Java if statement with example.
5. Types of operators in java with example.
6. Java HelloWorld program with example.
7. Top 100 java interview question and answer.
8. Java collection framework complete tutorial.
Java Switch Statement with Example Java Switch Statement with Example Reviewed by Prashant Srivastava on December 17, 2019 Rating: 5

No comments:

Powered by Blogger.