Switch case java exit
Like all expressions, switch expressions evaluate to a single value and can be used in statements. They may contain » case L -> » labels that eliminate the need for break statements to prevent fall through. You can use a yield statement to specify the value of a switch expression.
For background information about the design of switch expressions, see JEP 361.
Consider the following switch statement that prints the number of letters of a day of the week:
public enum Day < SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY; >// . int numLetters = 0; Day day = Day.WEDNESDAY; switch (day) < case MONDAY: case FRIDAY: case SUNDAY: numLetters = 6; break; case TUESDAY: numLetters = 7; break; case THURSDAY: case SATURDAY: numLetters = 8; break; case WEDNESDAY: numLetters = 9; break; default: throw new IllegalStateException("Invalid day: " + day); >System.out.println(numLetters);
It would be better if you could «return» the length of the day’s name instead of storing it in the variable numLetters ; you can do this with a switch expression. Furthermore, it would be better if you didn’t need break statements to prevent fall through; they are laborious to write and easy to forget. You can do this with a new kind of case label. The following is a switch expression that uses the new kind of case label to print the number of letters of a day of the week:
Day day = Day.WEDNESDAY; System.out.println( switch (day) < case MONDAY, FRIDAY, SUNDAY ->6; case TUESDAY -> 7; case THURSDAY, SATURDAY -> 8; case WEDNESDAY -> 9; default -> throw new IllegalStateException("Invalid day: " + day); > );
The new kind of case label has the following form:
case label_1, label_2, . label_n -> expression;|throw-statement;|block
When the Java runtime matches any of the labels to the left of the arrow, it runs the code to the right of the arrow and does not fall through; it does not run any other code in the switch expression (or statement). If the code to the right of the arrow is an expression, then the value of that expression is the value of the switch expression.
You can use the new kind of case label in switch statements. The following is like the first example, except it uses » case L -> » labels instead of » case L: » labels:
int numLetters = 0; Day day = Day.WEDNESDAY; switch (day) < case MONDAY, FRIDAY, SUNDAY ->numLetters = 6; case TUESDAY -> numLetters = 7; case THURSDAY, SATURDAY -> numLetters = 8; case WEDNESDAY -> numLetters = 9; default -> throw new IllegalStateException("Invalid day: " + day); >; System.out.println(numLetters);
A » case L -> » label along with its code to its right is called a switch labeled rule.
«case L:» Statements and the yield Statement
You can use » case L: » labels in switch expressions; a » case L: » label along with its code to the right is called a switch labeled statement group:
Day day = Day.WEDNESDAY; int numLetters = switch (day) < case MONDAY: case FRIDAY: case SUNDAY: System.out.println(6); yield 6; case TUESDAY: System.out.println(7); yield 7; case THURSDAY: case SATURDAY: System.out.println(8); yield 8; case WEDNESDAY: System.out.println(9); yield 9; default: throw new IllegalStateException("Invalid day: " + day); >; System.out.println(numLetters);
The previous example uses yield statements. They take one argument, which is the value that the case label produces in a switch expression.
The yield statement makes it easier for you to differentiate between switch statements and switch expressions. A switch statement, but not a switch expression, can be the target of a break statement. Conversely, a switch expression, but not a switch statement, can be the target of a yield statement.
It’s recommended that you use » case L -> » labels. It’s easy to forget to insert break or yield statements when using » case L: » labels; if you do, you might introduce unintentional fall through in your code.
For » case L -> » labels, to specify multiple statements or code that are not expressions or throw statements, enclose them in a block. Specify the value that the case label produces with the yield statement:
int numLetters = switch (day) < case MONDAY, FRIDAY, SUNDAY -> < System.out.println(6); yield 6; >case TUESDAY -> < System.out.println(7); yield 7; >case THURSDAY, SATURDAY -> < System.out.println(8); yield 8; >case WEDNESDAY -> < System.out.println(9); yield 9; >default -> < throw new IllegalStateException("Invalid day: " + day); >>;
Unlike switch statements, the cases of switch expressions must be exhaustive , which means that for all possible values, there must be a matching switch label. Thus, switch expressions normally require a default clause. However, for enum switch expressions that cover all known constants, the compiler inserts an implicit default clause.
In addition, a switch expression must either complete normally with a value or complete abruptly by throwing an exception. For example, the following code doesn’t compile because the switch labeled rule doesn’t contain a yield statement:
int i = switch (day) < case MONDAY -> < System.out.println("Monday"); // ERROR! Block doesn't contain a yield statement >default -> 1; >;
The following example doesn’t compile because the switch labeled statement group doesn’t contain a yield statement:
Because a switch expression must evaluate to a single value (or throw an exception), you can’t jump through a switch expression with a break , yield , return , or continue statement, like in the following example:
The switch Statement
Unlike if-then and if-then-else statements, the switch statement can have a number of possible execution paths. A switch works with the byte , short , char , and int primitive data types. It also works with enumerated types (discussed in Enum Types), the String class, and a few special classes that wrap certain primitive types: Character , Byte , Short , and Integer (discussed in Numbers and Strings).
The following code example, SwitchDemo , declares an int named month whose value represents a month. The code displays the name of the month, based on the value of month , using the switch statement.
public class SwitchDemo < public static void main(String[] args) < int month = 8; String monthString; switch (month) < case 1: monthString = "January"; break; case 2: monthString = "February"; break; case 3: monthString = "March"; break; case 4: monthString = "April"; break; case 5: monthString = "May"; break; case 6: monthString = "June"; break; case 7: monthString = "July"; break; case 8: monthString = "August"; break; case 9: monthString = "September"; break; case 10: monthString = "October"; break; case 11: monthString = "November"; break; case 12: monthString = "December"; break; default: monthString = "Invalid month"; break; >System.out.println(monthString); > >
In this case, August is printed to standard output.
The body of a switch statement is known as a switch block. A statement in the switch block can be labeled with one or more case or default labels. The switch statement evaluates its expression, then executes all statements that follow the matching case label.
You could also display the name of the month with if-then-else statements:
int month = 8; if (month == 1) < System.out.println("January"); >else if (month == 2) < System.out.println("February"); >. // and so on
Deciding whether to use if-then-else statements or a switch statement is based on readability and the expression that the statement is testing. An if-then-else statement can test expressions based on ranges of values or conditions, whereas a switch statement tests expressions based only on a single integer, enumerated value, or String object.
Another point of interest is the break statement. Each break statement terminates the enclosing switch statement. Control flow continues with the first statement following the switch block. The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered. The program SwitchDemoFallThrough shows statements in a switch block that fall through. The program displays the month corresponding to the integer month and the months that follow in the year:
public class SwitchDemoFallThrough < public static void main(String[] args) < java.util.ArrayListfutureMonths = new java.util.ArrayList(); int month = 8; switch (month) < case 1: futureMonths.add("January"); case 2: futureMonths.add("February"); case 3: futureMonths.add("March"); case 4: futureMonths.add("April"); case 5: futureMonths.add("May"); case 6: futureMonths.add("June"); case 7: futureMonths.add("July"); case 8: futureMonths.add("August"); case 9: futureMonths.add("September"); case 10: futureMonths.add("October"); case 11: futureMonths.add("November"); case 12: futureMonths.add("December"); break; default: break; >if (futureMonths.isEmpty()) < System.out.println("Invalid month number"); >else < for (String monthName : futureMonths) < System.out.println(monthName); >> > >
This is the output from the code:
August September October November December
Technically, the final break is not required because flow falls out of the switch statement. Using a break is recommended so that modifying the code is easier and less error prone. The default section handles all values that are not explicitly handled by one of the case sections.
The following code example, SwitchDemo2 , shows how a statement can have multiple case labels. The code example calculates the number of days in a particular month:
class SwitchDemo2 < public static void main(String[] args) < int month = 2; int year = 2000; int numDays = 0; switch (month) < case 1: case 3: case 5: case 7: case 8: case 10: case 12: numDays = 31; break; case 4: case 6: case 9: case 11: numDays = 30; break; case 2: if (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0)) numDays = 29; else numDays = 28; break; default: System.out.println("Invalid month."); break; >System.out.println("Number of Days = " + numDays); > >
This is the output from the code:
Using Strings in switch Statements
In Java SE 7 and later, you can use a String object in the switch statement’s expression. The following code example, StringSwitchDemo , displays the number of the month based on the value of the String named month :
public class StringSwitchDemo < public static int getMonthNumber(String month) < int monthNumber = 0; if (month == null) < return monthNumber; >switch (month.toLowerCase()) < case "january": monthNumber = 1; break; case "february": monthNumber = 2; break; case "march": monthNumber = 3; break; case "april": monthNumber = 4; break; case "may": monthNumber = 5; break; case "june": monthNumber = 6; break; case "july": monthNumber = 7; break; case "august": monthNumber = 8; break; case "september": monthNumber = 9; break; case "october": monthNumber = 10; break; case "november": monthNumber = 11; break; case "december": monthNumber = 12; break; default: monthNumber = 0; break; >return monthNumber; > public static void main(String[] args) < String month = "August"; int returnedMonthNumber = StringSwitchDemo.getMonthNumber(month); if (returnedMonthNumber == 0) < System.out.println("Invalid month"); >else < System.out.println(returnedMonthNumber); >> >
The output from this code is 8 .
The String in the switch expression is compared with the expressions associated with each case label as if the String.equals method were being used. In order for the StringSwitchDemo example to accept any month regardless of case, month is converted to lowercase (with the toLowerCase method), and all the strings associated with the case labels are in lowercase.
Note: This example checks if the expression in the switch statement is null . Ensure that the expression in any switch statement is not null to prevent a NullPointerException from being thrown.
Break and Continue & Switch-Case in Java
If you haven’t already had an overview of jump control statements in Java, please have a look at it here. In this article, we will start off with the break and continue statement!
Break statement:
- Break statement transfers control to the outside of the loop, thereby terminating the loop.
- The program control then resumes at the next statement following the loop.
- It can be used to terminate a case in the switch statement also.
Continue Statement:
- It is used to transfer the control to the beginning of the loop.
- The continue statement is the reverse of the break statement.
- Instead of terminating or exiting the block, it forces the next iteration of the loop to take place, skipping any code in between.
public class DemoC < public static void main(String[] args) < for (int i = 1; i System.out.println("Hello " + i); > > >
Why Selection Control Statements?
Have a look here, in this article. We will start off with Switch-case here!
Switch Case:
- It has 4 parts namely:
- Switch:
- It is the control keyword allowing us to make a choice out of multiple options.
- This contains the options along with case labels out of which we have to choose our relevant option.
- The execution starts with the match case.
- It is a jump statement which takes the control out of the loop.
- It prevents the unnecessary flow of control to the subsequent cases after the match case.
- The default code block gets executed when none of the cases matches with the case values present in the switch. It is optional.
Have a look at the below programs to understand better the implementation of switch statements.
import java.util.Scanner; public class MonthsName < public static void main(String[] args) < int month; System.out.println("Enter month number from 1 to 12 : "); Scanner sc = new Scanner(System.in); month = sc.nextInt(); switch(month) < case 1: System.out.println("January\n"); break; case 2: System.out.println( "February\n"); break; case 3: System.out.println("March\n"); break; case 4: System.out.println("April\n"); break; case 5: System.out.println("May\n"); break; case 6: System.out.println("June\n"); break; case 7: System.out.println("July\n"); break; case 8: System.out.println("August\n"); break; case 9: System.out.println("September\n"); break; case 10: System.out.println("October\n"); break; case 11: System.out.println("November\n"); break; case 12: System.out.println("December\n"); break; default: System.out.println("You have entered an invalid month number\n"); break; >> >
import java.util.Scanner; public class ArithmaticEquation < public static void main(String[] args) < System.out.println("Enter the values of a and b"); int a; int b; Scanner sc = new Scanner(System.in); a = sc.nextInt(); b = sc.nextInt(); int result; System.out.println("Enter the choice \n"); System.out.println("1. Addition \n 2. Subtraction \n 3. Multiplication"); int choice = sc.nextInt(); switch (choice) < case 1: System.out.println("You chose Addition"); result = a + b; System.out.println("Result is: " + result); break; case 2: System.out.println("You chose Subtraction"); result = a - b; System.out.println("Result is: + result); break; case 3: System.out.println("You chose Multiplication"); result = a * b; System.out.println("Result is: " + result); break; default: System.out.println("You have entered an invalid choice! Try again!"); break; >> >
Why is the break statement necessary after each case?
- Writing a break statement is optional. But it becomes necessary if you want to execute only the match option.
- If there is no break statement, all consecutive blocks of codes present after the match case will be executed.
- The break statement at the end of each case causes the switch statement to exit.
- As a result, only the match option will be executed.
Is it necessary to use default case in switch case?
- In most of the cases, switch statements have a default case to ‘catch’ an unexpected value.
- It is a type of exception handling for switch statements.
- If you enter an invalid option, then instead of throwing an error or stopping the program, the control moves to the default block.
- Thus if there is no match with any case label, the code block associated with the default keyword is executed.
In the next article, we will learn about the arrays and how to operate on them. The article will also contain a brief introduction to the String class available in Java. The article will cover basic array creation, operations on array elements, string methods, string operations.