Introduction
Conditional statements are used to control the flow in your application. In this post, we'll look at if/else statements, switch statements, and the newer pattern matching for switch expressions.
If and Else Statements
The if statement is the most basic form of conditional execution. It allows you to execute different code blocks based on a true or false condition.
Basic If Statement
The most basic version is just a single if statement. The code between the {}
is only executed if the condition is true.
In the next example that would be when the temperature is higher than 30.
|
|
You can also add an else{}
that would be executed when the if statement is not true.
Using Logical AND (&&)
The AND operator requires both conditions to be true for the code block to execute. This is useful when you need to check multiple conditions at the same time.
|
|
Using Logical OR (||)
The OR operator executes the code between the {}
if at least one of the condition is true. This is useful when you execute code when one of the
two conditions makes it oke to run the code.
|
|
In this case the if
statement will run because isHoliday
is true.
Combining AND and OR Operators
For more complex logic, you can combine AND and OR operators. You can also use parentheses to control the order of evaluation. In the following example both operators are used:
|
|
In this example first the condition between the parentheses is evaluated. The result is than used for the surrounding statement. In this case the condition is true.
Else-If
When you need to check multiple conditions in one after each other, use an else-if. The conditions are checked in order until one is true, or the else statement is executed. In the following example the time is greater than 12 but still less than twelve making the first if statement false.
|
|
In this example the second if statement is true.
Nested If Statements with Multiple Conditions
For complex decision, you can nest if statements and combine them with logical operators. In the following example you can see an If statement inside another if statement.
|
|
When doing things like this it is important to keep readability in mind. It can be quite hard to debug when multiple if statements are nested.
Switch Statements
Switch statements provide a way to handle multiple conditions, especially when comparing a single variable against several possible cases.
Basic Switch Statement
The traditional switch statement is ideal for comparing a single value against multiple constants. Note that each case requires a break
statement. If the break
wasn't there the next case would also be evaluated.
|
|
In the previous example you can see how a String
is compared against multiple cases. When no match is found the default will be executed.
Switch with Fall-Through Cases
Sometimes you want multiple cases to execute the same code. You can do this by not using the break statement between cases.
|
|
In the previous example monday to friday all have the same behaviour.
Switch Expression with Arrow Syntax
Java 12 introduced switch expressions with arrow syntax, allowing more concise code and automatic returns.
|
|
This code is more concise than the previous examples. This makes reading it easier, and you can't forget the return/ break statement.
Pattern Matching for Switch Expressions
Pattern matching for switch expressions was added in Java 21. This makes writing switch statements a lot more fluent and easier to read as well.
Basic Type Pattern Matching
You can use pattern matching to match the type of an object and handle each case differently.
|
|
In the previous example an Object is matched against different cases and based on that type some code is executed. Notice that you don't have to do any casting inside those cases.
Pattern Matching with Guards
Guard patterns allow you to add additional conditions to your case statements, making them more specific.
|
|
This is handy when the type is the same, but you want to do something based on a type but also need to take the value of that object into account.
Pattern Matching classes
Pattern matching also works with classes. If you want to check a value of a class or record that also works. In the following example the values of the record are used to make the cases more specific.
|
|
This would also work with Classes.
Conclusion
In this post, we looked at different ways of controlling the flow of your application. Depending on the situation, number of cases, and readability you can use any of these options to control the flow.