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
true
only 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
true
when 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
false
if the expression is true, andtrue
if 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 if
keyword 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 if
statement with a Boolean expression that is opposite the first, but Java provides a shortcut called the if
/else
conditional.
- The
if
/else
conditional will run the block of code associated with theif
statement if its Boolean expression evaluates totrue
. - Otherwise, if the Boolean expression evaluates to
false
, it will run the block of code after theelse
keyword.
Here’s an example of if
/else
syntax:
|
|
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
if
statement evaluates totrue
, it will run the code block that directly follows. - Otherwise, if the Boolean expression after the
else if
statement evaluates totrue
, the code block that directly follow will run. - Finally, if all previous Boolean expressions evaluate to
false
, the code within theelse
block will run.
Here’s an example of control flow with the if
/else if
/else
statement:
|
|
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
/else
statements 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 switch
statement, which helps keep code organized and less wordy.
The switch statement is used as follows:
|
|