Conditional blocks
Navigate Language Fundamentals topic: ) |
Conditional blocks allow a program to take a different path depending on some condition(s). These allow a program to perform a test and then take action based on the result of that test. In the code sections, the actually executed code lines will be highlighted.
If
editThe if
block executes only if the boolean expression associated with it is true. The structure of an if
block is as follows:
if (boolean expression1) {
} |
Here is a double example to illustrate what happens if the condition is true and if the condition is false:
|
|
If only one statement is to be executed after an if block, it does not have to be enclosed in curly braces. For example, if (i == 0) i = 1; is a perfectly valid portion of Java code. This works for most control structures, such as else and while . However Oracle's Java Code Conventions explicitly state that the braces should always be used.
|
If/else
editThe if
block may optionally be followed by an else
block which will execute if that boolean expression is false. The structure of an if
block is as follows:
if (boolean expression1) {
} else {
} |
If/else-if/else
editAn else-if
block may be used when multiple conditions need to be checked. else-if
statements come after the if
block, but before the else
block. The structure of an if
block is as follows:
if (boolean expression1) {
} else if (boolean expression2) {
} else {
} |
Here is an example to illustrate:
|
|
Keep in mind that only a single block will execute, and it will be the first true condition.
All the conditions are evaluated when if
is reached, no matter what the result of the condition is, after the execution of the if
block:
|
|
Conditional expressions
editConditional expressions use the compound ?:
operator. Syntax:
boolean expression1 ? expression1 : expression2 |
This evaluates boolean expression1
, and if it is true
then the conditional expression has the value of expression1
; otherwise the conditional expression has the value of expression2
.
Example:
Code section 3.24: Conditional expressions.
String answer = (p < 0.05)? "reject" : "keep";
|
This is equivalent to the following code fragment:
Code section 3.25: Equivalent code.
String answer;
if (p < 0.05) {
answer = "reject";
} else {
answer = "keep";
}
|
Switch
editThe switch
conditional statement is basically a shorthand version of writing many if
...else
statements. The switch
block evaluates a char
, byte
, short
, or int
(or enum
, starting in J2SE 5.0; or String
, starting in J2SE 7.0), and, based on the value provided, jumps to a specific case
within the switch block and executes code until the break
command is encountered or the end of the block. If the switch value does not match any of the case values, execution will jump to the optional default
case.
The structure of a switch
statement is as follows:
switch (int1 or char1 or short1 or byte1 or enum1 or String value1) {
} |
Here is an example to illustrate:
|
|
If a case does not end with the break
statement, then the next case will be checked, otherwise the execution will jump to the end of the switch
statement.
Look at this example to see how it's done:
|
|
Starting in J2SE 5.0, the switch
statement can also be used with an enum
value instead of an integer.
Though enums
have not been covered yet, here is an example so you can see how it's done (note that the enum constants in the cases do not need to be qualified with the type:
|
|
Starting in J2SE 7.0, the switch
statement can also be used with an String
value instead of an integer.
|
|