7.6 Flow Control: Conditional Switch Structure
The conditional structure switch
is one of the fundamental tools in Java programming to control the flow of a program. It is used when we have a limited number of discrete values that a variable can take and we want to execute different blocks of code depending on the value of that variable. switch
provides a cleaner, more readable way to write a series of if-else-if
statements that compare the same variable.
How the Switch Works
The switch
works as a kind of multiple selection for flow control. It evaluates the value of a variable or expression and then looks for a match between that value and a series of programmer-defined cases. If a match is found, the block of code associated with that case is executed. Java allows the use of switch
with primitive types such as int
, byte
, short
, char< /code>, and with the classes
String
, Enum
and some wrapper classes of primitive types, such as Integer
and Character
.
Basic Switch Syntax
The basic syntax of switch
is as follows:
switch (expression) {
case value1:
// Code block for value1
break;
case value2:
// Code block for value2
break;
// you can have any number of cases
default:
// Default code block (optional)
}
In the example above, the expression inside the parentheses of switch
is evaluated once, and its result is compared with the values specified in the cases (case
). If a match is found, the associated block of code is executed. The break
command is used to exit the switch
after executing a block of code in a case. If break
is omitted, the program will continue to execute subsequent cases until it encounters a break
or until the end of the switch
. The default
case is optional and is executed if none of the cases match the evaluated expression.
Example of Switch in Java
int month = 4;
String monthName;
switch (month) {
case 1:
monthName = "January";
break;
case 2:
monthName = "February";
break;
case 3:
monthName = "March";
break;
case 4:
monthName = "April";
break;
case 5:
monthName = "May";
break;
case 6:
monthName = "June";
break;
// ... other cases ...
default:
monthName = "Invalid month";
break;
}
System.out.println("The month is " + monthName);
In this example, the variable mes
is evaluated by switch
, and the case corresponding to the value of mes
is executed. Since the value of month
is 4, the code block corresponding to case 4 is executed, and the variable nomeDoMes
is set to "April".
Switch without Break
Although the use of break
is the most common and recommended, there are situations in which it may be useful to omit it. For example, when multiple cases must execute the same block of code, you can group them together to avoid code repetition. However, great care must be taken when doing this to avoid logical errors.
Switch with Strings
As of Java 7, it is possible to use switch
with strings. This makes writing code easier when we are dealing with variables that store text. See an example:
String level = "Expert";
switch (level) {
case "Beginner":
System.out.println("Welcome to beginner level!");
break;
case "Intermediate":
System.out.println("You already have some experience.");
break;
case "Expert":
System.out.println("You are an advanced user!");
break;
default:
System.out.println("Unknown level!");
break;
}
In the example above, switch
checks the contents of the variable level
and executes the block of code corresponding to the value found.
Final Considerations
The switch
conditional structure is a valuable and efficient tool for controlling the flow of execution in a Java program. It should be used when we have a clear and limited set of values that a variable can take and we want to perform different actions for each of these values. Correct use of switch
can make code cleaner, more readable, and easier to maintain.
However, it is important to remember that switch
does not completely replace the conditional structures if
and else.
switch
is more suitable for situations where we are comparing a single variable with specific values, while if
is more flexible and can be used to evaluate more complex expressions and multiple conditions .
With practice and a proper understanding of when and how to use switch
, you can write more efficient and organized Java programs, improving the quality of your code and the efficiency of your development process.