JAVA switch case: Introduction
JAVA switch case statement is used, like the “if else” statement in making decision in our programs.
The JAVA switch case statement checks if a variable is equal to one value
among a list of values.
The JAVA switch case
statement works with a variable whose type is the following:
- byte
- char
- int primitive data types (int, long, short)
- enumerated types
- Wrapper Classes : Character, Byte, Short, and Integer
- String (Java 7 supports
also switch statements with Strings)
The syntax of the switch case statement is the following:
switch ( <expression> ) {
case <constant1>:
<statement_or_block>*
[break;]
case <constant2>:
<statement_or_block>*
[break;]
default:
<statement_or_block>*
[break;]
}
The switch statement works like this:
- <expression> is compared to each value of
constanti in order
- If <expression> is equal to constanti,
the “case constanti” clause will be executed. Otherwise, the
next case clause is checked.
- If <expression> is not equal to any
constanti, the default clause will be executed.
- The break keyword finishes the switch statement
Example:
This example displays the label of the month based on its number.
The month number 8 is August. So, the program displays August.
public static void main(String[] args) {
int month = 8;
switch (month) {
case 1: System.out.println("January"); break;
case 2: System.out.println("February");
break;
case 3: System.out.println("March"); break;
case 4: System.out.println("April");
break;
case 5:
System.out.println("May");
break;
case 6:
System.out.println("June");
break;
case 7:
System.out.println("July");
break;
case 8:
System.out.println("August"); break;
case 9: System.out.println("September"); break;
case 10:
System.out.println("October");
break;
case 11:
System.out.println("November");
break;
case 12:
System.out.println("December");
break;
default:
System.out.println("Invalid month");
break;
}
}
}
We can surely do the same thing using the “if else” statement. But, we
will write much more lines of code.
Surely, this has a bad influence on the readability of our program.
public static void main(String[] args) {
int month = 8;
if (month == 1) {
System.out.println("January");
}
else if (month == 2){
System.out.println("February");
}
else if (month == 3){
System.out.println("March");
}
else if (month == 4){
System.out.println("April");
}
else if (month == 5){
System.out.println("May");
}
else if (month == 6){
System.out.println("June");
}
else if (month == 7){
System.out.println("July");
}
else if (month == 8){
System.out.println("August");
}
else if (month == 9){
System.out.println("September");
}
else if (month == 10){
System.out.println("October");
}
else if (month == 11){
System.out.println("November");
}
else if (month == 12){
System.out.println("December");
}
else{
System.out.println("Invalid month");
}
}
}
JAVA switch case and the break keyword
The break keyword is necessary in order to make the switch case
statement work properly.
In fact, when the program encounters the break keyword, it quits the
switch statement and executes the instructions which come after the switch
statement.
When we omit this keyword, we don’t get the expected result.
If we omit the break in the “case 1:” clause, the program still work as
expected. But if we remove the break in the “case 8:” clause, we will get a bad
behavior:
public static void main(String[] args) {
int month = 8;
switch (month) {
case 1: System.out.println("January"); break;
case 2: System.out.println("February"); break;
case 3: System.out.println("March");
break;
case 4: System.out.println("April");
break;
case 5:
System.out.println("May");
break;
case 6:
System.out.println("June");
break;
case 7:
System.out.println("July");
break;
case 8: System.out.println("August");
case 9: System.out.println("September");
break;
case 10:
System.out.println("October");
break;
case 11:
System.out.println("November"); break;
case 12:
System.out.println("December"); break; default:
System.out.println("Invalid month"); break;
}
}
}
The output that we get from the program above is the following:
August
September
So, the “case 8:” was executed and the “case 9” also.
The break in the “case 9:” fished the switch statement.
If we remove the break keyword from all the “case” clauses after “case 9:”
all the case clauses will be executed.
JAVA switch case VS JAVA if else statement:
The switch case statement offers better programs in term of readability.
But, the case clause doesn’t support boolean expressions (equality, inequality,
value in range, …).
In fact, the switch statement tests expressions based only on a single
integer, enumerated value, or String object.
The if else statement supports these kind of conditions.
Post a Comment