Control flow statements in Java — conditionals and loops — are very straightforward.
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.
|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|
|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|
Let’s consider an if statement with no
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.
Adding an else clause, we have:
An else if construction in Java:
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
Here is a quick example of a
In the example above, here’s the output if a user enters the number
Enter an integer: 4 Thursday
And the output if that user enters
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:
Additionally, if break statements are omitted from the individual
cases on accident, a behavior known as
is carried out. Fallthrough can be quite unintuitive, and is only
desirable in very specific circumstances. We will discuss
statements in more detail in the upcoming loop section. For now, just
know that when used in a
switch block, they terminate the
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
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
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
that ends up being printed will reflect the last executed statement in
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
Enter an integer: 4 Saturday
Check Your Understanding
When does fallthrough occur in Java?
- Omitting an
elseclause from a conditional.
- Omitting an
elseclause from switch statement.
- Omitting a
defaultcase from a
- Omitting a
breakline from a
Given the code above, what prints if the user enters
no after the prompt?
Greetings normie. Are you an alien?
Greetings cadet. Greetings normie.