evaluate Boolean expressions that use relational operators in program code
Recall that boolean variables or expressions can only have true or false values.
TESTING EQUALITY (==)
The operators == and != (not equal) can be used to compare values and they return boolean values (true or false).
IMPORTANT: Remember that = is used to initialize or reassign the value of a variable. == is used to test if a condition is true or false and DOESN'T change it's value!
When you see =, think "gets assigned" and when you see ==, think "is the value on the left equal to the value on the right?"
Here is some code that shows how == is used with primitive types like int:
We technically can use == or != to test if two reference values, like Turtle or String objects, refer to the same object.
In the figure below, we are creating two separate Turtle objects called juan and mia.
They do not refer to the same object or turtle.
Then, we create a reference variable called friend that is set to mia.
The turtle mia will have two ways (references or aliases) to name her - she's both mia and friend, and these variables refer to the same Turtle object in memory.
If two reference variables refer to the same object like the turtle on the right in the image below, the test with == will return true which you can see in the code below.
The following code demonstrates what happens when primitive types and reference types are compared with ==.
RELATIONAL OPERATORS
The Relational Operators below in Java are used to compare numeric values or arithmetic expressions.
Although some programming languages allow using relational operators like < and > to compare strings, Java only uses these operators for numbers, and uses the methods compareTo() and equals() for comparing String values.
< less than
> greater than
<= less than or equal to
>= greater than or equal to
== equals
!= does not equal
The following code shows how these relational operators are used:
TESTING WITH MOD (%)
The modulus operator (%) returns the remainder when the value on the left is divided by the value on the right.
Let's make sure we know how this works with a several examples:
a) 6 % 2 = ?e) 7 % 5 = ?i) 1 % 2 = ?
b) 7 % 2 = ?f) 8 % 3 = ?j) 5 % 7 = ?
c) 18 % 2 = ?g) 17 % 4 = ?k) 21 % 22 = ?
d) 21 % 2 = ?h) 24 % 7 = ?l) 1080 % 2160 = ?
Here are the answers:
a) 6 % 2 = 0e) 7 % 5 = 2i) 1 % 2 = 1
b) 7 % 2 = 1f) 8 % 3 = 2j) 5 % 7 = 5
c) 18 % 2 = 0g) 17 % 4 = 1k) 21 % 22 = 21
d) 21 % 2 = 1h) 24 % 7 = 3l) 1080 % 2160 = 1080
So, keep in mind that any EVEN number % 2 will result in a 0 being returned in the console.
Any ODD number % 2 will result in a 1 being returned in the console.
If the number on the left is LESS THAN the number on the right, the number on the left will be returned in the console because the number doesn't divide in evenly (it's ALL REMAINDER).
A couple more things...
Let's say you have a variable named number and you want to test if it is odd. You could use:
number % 2 == 1
or
number % 2 > 0
And if you wanted to test if it is a multiple of some value, x, you could use:
number % x == 0
for example, if you want to test if it is a multiple of ten you could type:
number % 10 == 0
Or what if you have a really big number and you want the last digit on the right?
135252 % 10 == 2
135252 % 100 == 52
135252 % 1000 == 252
Or, let's say you are converting 340 min to hours... 340 / 60 = 5.66666666....
340 % 60 = 40 (0.66666666... hours is 40 min)
You can also use % if you have a limit in the value, and you need to wrap around to the front if the value goes over the limit, for example:
num % limit
If the limit is 100
num % 100 would prevent num from ever exceeding 100...
The modulo operator (%) has been used quite a bit on the exam, so make sure you are very familiar with it!
SUMMARY
Primitive values and reference values can be compared using relational operators (i.e., == and !=) in Java.
Arithmetic expression values can be compared using relational operators (i.e., <, >, <=, >=) in Java.
An expression involving relational operators evaluates to a Boolean value of true or false.
EVIDENCE
1) Complete the following Google Form. This form must be 100% correct and includes released AP practice questions. To stop working and return later, hit submit! You can "edit your response" and continue where you left off.
2) MC Exam Practice: This lesson has additional practice problems which can be found on the MC Exam Practice page with an answer key! Use ctrl + f to search for different objectives throughout the year. You could look at these questions now, or, save these practice questions for later and use them to review! The choice is yours.