The switch
statement is used to evaluate a variable or an expression against a set of constant values and execute different statements based on the matching value. The switch
statement provides a more concise way of testing a variable against multiple values than using a series of if-else
statements.
The basic syntax of the switch
statement is as follows:
switch (variable)
{
case value1:
// statement(s) to execute if variable is equal to value1
break;
case value2:
// statement(s) to execute if variable is equal to value2
break;
case value3:
// statement(s) to execute if variable is equal to value3
break;
// more cases can be added here
default:
// statement(s) to execute if none of the above cases match
}
The switch
statement evaluates the value of the variable
against the case
labels, which are constant values. If a matching case
is found, the statements inside that case
are executed until the break
statement is encountered, which exits the switch
block. If none of the case
labels match the value of the variable
, the statements inside the default
the block is executed.
Here’s an example that demonstrates the use of the switch
statement:
int dayOfWeek = 4;
String dayName;
switch (dayOfWeek)
{
case 1:
dayName = "Sunday";
break;
case 2:
dayName = "Monday";
break;
case 3:
dayName = "Tuesday";
break;
case 4:
dayName =
"Wednesday";
break;
case 5:
dayName = "Thursday";
break;
case 6:
dayName = "Friday";
break;
case 7:
dayName = "Saturday";
break;
default:
dayName = "Invalid day";
}
System.out.println("Today is " + dayName);
In this example, the program uses a switch
statement to determine the name of the day based on the dayOfWeek
variable. Since the value of dayOfWeek
is 4
, the case 4
label matches and the program assigns "Wednesday"
to the dayName
variable. Finally, the program prints out "Today is Wednesday"
.
Fall through
Fall through refers to the behavior where the control flow of the program continues to the next case
label after the matching case
label, even if there is no break
statement.
Here’s an example that demonstrates the fall through behavior in a switch
statement:
int num = 2;
String numWord;
switch (num)
{
case 1:
case 2:
case 3:
numWord = "small";
break;
case 4:
case 5:
case 6:
numWord = "medium";
break;
case 7:
case 8:
case 9:
numWord = "large";
break;
default:
numWord = "unknown";
}
System.out.println(num + " is a " + numWord + " number.");
In this example, the program uses a switch
statement to determine whether a given num
is small, medium, or large. The first case
label matches for num
values of 1, 2, or 3. Instead of using a break
statement at the end of the case
block, the control flow falls through to the next case
label. The second case
label matches for num
values of 4, 5, or 6, and so on.
If there is no break
statement at the end of a case
block and the control flow falls through to the next case
label, the statements in that case
block are also executed, even if the value of the variable does not match that case
label. For example, if num
were 2
, both the case 1
and case 2
blocks would be executed, even though the value of num
only matches case 2
.
Therefore, it is important to be aware of the fall through behavior and use break
statements appropriately in a switch
statement to avoid unintended behavior in your program.