Using Boolean Expressions
As you saw in the algorithm section, an algorithm is comprised of sequential steps, conditionals, and iterative processes. Sequential steps are those done in order. Conditionals allow the program to behave in different ways based on decisions. Iterative processes repeat series of steps.
In this tutorial we will focus on Boolean expressions, which are critical to conditionals and iterative processes.
Boolean Expressions
Recall that a boolean
variable is assigned either true
or false
. A Boolean expression is any expression that can be evaluated to a boolean value, true
or false
. They are comprised of relationship operators and logic operators.
Relationship Operators
The relationship operators compare values and evaluate to either true
or false
. They are as follows:
Operator |
Name |
Description |
Example |
>
|
greater than |
evaluates to true if the value on the left is greater than the value on the right and false otherwise. |
3 > 5 evaluates to false
5 > 3 evaluates to true
5 > 5 evaluates to false since the values are equal
|
<
|
less than |
evaluates to true if the value on the left is less than the value on the right and false otherwise. |
3 < 5 evaluates to true
5 < 3 evaluates to false
5 < 5 evaluates to false since the values are equal
|
>=
|
greater than or equal to |
evaluates to true if the value on the left is greater than or equal to the value on the right and false otherwise. |
3 >= 5 evaluates to false
5 >= 3 evaluates to true
5 >= 5 evaluates to true since the values are equal
|
<=
|
less than or equal to |
evaluates to true if the value on the left is less than or equal to the value on the right and false otherwise. |
3 <= 5 evaluates to true
5 <= 3 evaluates to false
5 <= 5 evaluates to true since the values are equal
|
==
|
equal to |
evaluates to true if the values are equal to each other and false otherwise. |
3 == 5 evaluates to false
5 == 5 evaluates to true since the values are equal
|
!=
|
not equal to |
evaluates to true if the values are not equal to each other and false otherwise. |
3 != 5 evaluates to true
5 != 5 evaluates to false
|
Try these out in the Java Playground. Take a few minutes to change the values and see the new results.
The values being compared could be variables, expressions, or values. In these examples, we have integrated variables and expressions. Take a few minutes to change the values and see the new results.
Logic Operators
Logic operators are used to combine boolean expressions. The most common ones are listed in the table below.
Operator |
Name |
Description |
Example |
!
|
not |
evaluates to true if the boolean expression is false and false if the boolean expression is true . |
!(3 > 5) evaluates to true since 3 > 5 evaluates to false .
!(5 > 3) evaluates to false since 5 > 3 evaluates to true .
|
&&
|
and |
evaluates to true if both boolean expressions evalute to true and false if one or both boolean expressions evaluate to false . |
true && true evaluates to true .
true && false evaluates to false .
false && true evaluates to false .
false && false evaluates to false .
|
||
|
or |
evaluates to true if at least one of the boolean expressions evalute to true and false if both boolean expressions evaluate to false . |
true || true evaluates to true .
true || false evaluates to true .
false || true evaluates to true .
false && false evaluates to false .
|
Try these out in the Java Playground. Take a few minutes to change the values and see the new results.
!(3 < 5)
is equivalent to 3 >= 5
. When the not (!
) operator is used on an expression with a relationship operator, the equivalent expression is uses the opposite relational operator. The opposite of less than is greater than or equal to. The opposite of greater than is less than or equal to.
DeMorgan's Law
DeMorgan's Law applies in situations when a not (!
) operator is being used in combination with either the and (&&
) or the or (||
) operators.
Rule |
Description |
Example
|
!(value1 && value2)
|
evaluates to !value1 || !value2 where value1 and value2 are boolean expressions. |
!((5 > 7) && (3 !=0)) is equivalent to (5 <= 7) || (3 == 0) |
!(value1 || value2)
|
evaluates to !value1 && !value2 where value1 and value2 are boolean expressions. |
!((val + 5 <= 0) || (5 == 5)) is equivalent to (val + 5 > 0) && (5 != 5) |
Short-Circuit Evaluation
If the value of a boolean expression that involves a logic operator can be determined based on only the first boolean expression, the second boolean expression is not evaluated.
And (&&) Operator Short-Circuit
If the first expression evaluates to false
, the expression with the &&
operator evaluates to false
.
This example is a popular use of short-circuit for &&
. If the value of numGrades
is 0
, we would get a divide by 0 error when attempting totalGrades / numGrades
. Checking that numGrades
is not 0
before performing division can prevent this error. Try it out! Run the code as is, and then change the value of numGrades
to 0
.
Comparing Objects
To compare objects, you will want to use a method. The equals
method, which is avaiable for all classes, can be used. Not all classes have a specific implementation of the equals
method, so you will want to look at the API to see if there is an implementation for the class.
String Class Equals Method
boolean equals(Object anObject)
This method returns
true
if the value of this String
is the same as the String
value of anObject
. NOTE: All classes extend the Object
class, meaning a String
is an Object
.
false
if the value of this String
is not the same as theString
value of anObject
.
LocalDate Class Equals Method
boolean equals(Object obj)
This method returns
true
if the value of this LocalDate
has the same date as obj
.
false
if the value of this LocalDate
does not have the same date as obj
.
Resources