Conditionals
Control flow statements in Java — conditionals and loops — are very straightforward.
Operators
Before we review the syntax for conditionals, let’s go over the comparison and logical operators that we need to use in control flow statements.
Comparison Operators
Operator | Description |
---|---|
== | Checks if two items are equal |
!= | Checks if two items are not equal |
< | Checks if item on left is lesser than item on right |
<= | Checks if item on left is lesser than or equal to item on right |
> | Checks if item on left is greater than item on right |
>= | Checks if item on left is greater than or equal to item on right |
Logical Operators
Operator | Description |
---|---|
&& | Combines two expressions with AND, returns true if both statements are true |
|| | Combines two expressions with OR, returns true if at least one of the statements is true |
! | Reverses the evaluation of the operand, returns false if the result is true |
if
Statements
Let’s consider an if statement with no else
clause.
In Java this pattern is simply written as:
|
|
You can see that in Java the curly braces define a block. Parentheses around the condition are required.
if else
Adding an else clause, we have:
|
|
else if
An else if construction in Java:
|
|
switch
Statements
Java also supports a switch statement that acts something like an
else if
statement under certain conditions, called cases. The
switch
statement is not used very often, and we generally recommend you
avoid using it. It is not as powerful as the else if
model because the
switch
variable can only be compared for equality with a very small class
of types.
Here is a quick example of a switch
statement:
|
|
In the example above, here’s the output if a user enters the number 4
.
Enter an integer: 4
Thursday
And the output if that user enters 10
? Below:
Enter an integer: 10
Int does not correspond to a day of the week
Here’s how the above example looks using the else if
construction:
|
|
Fallthrough
Additionally, if break statements are omitted from the individual
cases on accident, a behavior known as
fallthrough
is carried out. Fallthrough can be quite unintuitive, and is only
desirable in very specific circumstances. We will discuss break
statements in more detail in the upcoming loop section. For now, just
know that when used in a switch
block, they terminate the switch
statement they are in, so the flow of control in your program moves to
the next statement after the switch block.
Here’s a quick example of how fallthrough works:
|
|
This time, without the break
statements in each case
, if the
user enters 4
, they will see the default output:
Enter an integer: 4
Int does not correspond to a day of the week
This is because after the switch
statement matches the
case
for 4
and assigns the value Thursday
to the variable
day
, it proceeds to execute every statement in every case that
follows, all the way through the default
case. So the String
that ends up being printed will reflect the last executed statement in
the switch
block.
Along similar lines, consider this variation on the code block above:
|
|
Here, we have a break
statement in case 6
after day = "Saturday";
.
If the user enters 4
, the execution will fallthrough until it reaches that
break
statement and Saturday
is printed instead of Thursday
.
The output:
Enter an integer: 4
Saturday
Check Your Understanding
When does fallthrough occur in Java?
- Omitting an
else
clause from a conditional. - Omitting an
else
clause from switch statement. - Omitting a
default
case from aswitch
statement. - Omitting a
break
line from aswitch
statement.
|
|
Given the code above, what prints if the user enters no
after the prompt?
Greetings cadet.
Greetings normie.
Greetings normie. Are you an alien?
Greetings cadet. Greetings normie.