Boolean operators
There are three Boolean operators that we will explore. Let’s start with the first one: and.
- The and operator is represented in Java by
&&. - It returns a boolean value of
trueonly when the expressions on both sides of&&are true.
- The and operator is represented in Java by
Great! The second Boolean operator that we will explore is called or.
- The or operator is represented in Java by
||. - It returns a Boolean value of
truewhen at least one expression on either side of||is true.
- The or operator is represented in Java by
The final Boolean operator we will explore is called not.
- The not operator is represented in Java by
!. - It will return the opposite of the expression immediately after it. It will return
falseif the expression is true, andtrueif the expression is false.
- The not operator is represented in Java by
The precedence of each Boolean operator is as follows:
!is evaluated first&&is evaluated second||is evaluated third
If Statement
Let’s get familiar with how relational, equality, and Boolean operators can be used to control the flow of our code.
We’ll start by exploring the ifstatement.
- In Java, the keyword if is the first part of a conditional expression.
- It is followed by a Boolean expression and then a block of code. If the Boolean expression evaluates to
true, the block of code that follows will be run.
Here’s an example of the ifstatement used with a conditional expression:
| |
If-Else Statement
Sometimes we execute one block of code when the Boolean expression after the ifkeyword is true. Other times we may want to execute a different block of code when the Boolean expression is false.
We could write a second ifstatement with a Boolean expression that is opposite the first, but Java provides a shortcut called the if/elseconditional.
- The
if/elseconditional will run the block of code associated with theifstatement if its Boolean expression evaluates totrue. - Otherwise, if the Boolean expression evaluates to
false, it will run the block of code after theelsekeyword.
Here’s an example of if/elsesyntax:
| |
If-ElseIf-Else Statement
Good work! In some cases, we need to execute a separate block of code depending on different Boolean expressions. For that case, we can use the if/else if/else statement in Java.
- If the Boolean expression after the
ifstatement evaluates totrue, it will run the code block that directly follows. - Otherwise, if the Boolean expression after the
else ifstatement evaluates totrue, the code block that directly follow will run. - Finally, if all previous Boolean expressions evaluate to
false, the code within theelseblock will run.
Here’s an example of control flow with the if/else if/elsestatement:
| |
Ternary Conditional
if/else statements can become lengthy even when you simply want to return a value depending on a Boolean expression. Fortunately, Java provides a shortcut that allows you to write if/elsestatements in a single line of code. It is called the ternary conditional statement.
The term ternary comes from a Latin word that means “composed of three parts”.
These three parts are:
- A Boolean expression
- A single statement that gets executed if the Boolean expression is true
- A single statement that gets executed if the Boolean expression is false
Here is an example of a ternary conditional statement:
| |
Switch Statement
The conditional statements that we have covered so far require Boolean expressions to determine which code block to run. Java also provides a way to execute code blocks based on whether a block is equal to a specific value. For those specific cases, we can use the switchstatement, which helps keep code organized and less wordy.
The switch statement is used as follows:
| |