3.1. Boolean Expressions

Boolean variables or expressions can only have true or false values. Primitive values like ints and reference values like Strings can be compared using the operators == and != (not equal) to return boolean values.

Note

One = sign changes the value of a variable. Two == equal signs are used to test if a variable holds a certain value, without changing its value!

coding exercise Coding Exercise

What will the code below print out? Try to guess before you run it! Note that 1 equal sign (=) is used for assigning a value and 2 equal signs (==) for testing values.

coding exercise Coding Exercise

We can also use == to test if two reference values refer to the same object. What will the code below print out? Try to guess before you run it! Here we are testing if the Strings refer to the same object in memory.

The Relational Operators below in Java are used to compare numeric values or arithmetic expressions. Although some programming languages allow using relational operators like < to compare strings, Java only uses these operators for numbers, and uses the string methods compareTo() and equals() for comparing String values.

If you have trouble telling < and > apart, think of a number line and think of < and > as arrows; < (less than) points towards 0 and smaller numbers on the number line and > (greater than) points towards the larger numbers on the number line. With <= and >=, remember to write the two symbols in the order that you would say them “less than” followed by “or equal to”.

coding exercise Coding Exercise

Try to guess what the code below will print out before you run it.

exercise Check your understanding

    3-1-1: Drag the boolean expression from the left and drop it on what it is testing on the right. Click the "Check Me" button to see if you are correct. Review the relational operators above.
  • x > 0
  • x is positive
  • x == y
  • x equals y
  • x < 0
  • x is negative
  • x != y
  • x does not equal y
  • x < y
  • x is less than y
  • x > y
  • x is greater than y
  • x >= y
  • x is greater than or equal to y

Here are some boolean expressions that are very useful in coding:

// Test if a number is positive
(number > 0)
//Test if a number is negative
(number < 0)
//Test if a number is even by seeing if the remainder is 0 when divided by 2
(number % 2 == 0)
//Test if a number is odd by seeing if there is a remainder when divided by 2
(number % 2 > 0)
//Test if a number is a multiple of x (or divisible by x with no remainder)
(number % x == 0)

Try the expressions containing the % operator below to see how they can be used to check for even or odd numbers. All even numbers are divisible (with no remainder) by 2.

The modulo operator has been used quite a bit on the AP CS A exam, so you should be familiar with it.

  • Use it to check for odd or even numbers (num % 2 == 1) is odd and (num % 2 == 0) is even. Actually, you can use it to check if any number is evenly divisible by another (num1 % num2 == 0)

  • Use it to get the last digit from an integer number (num % 10 = last digit on right).

  • Use it to get the number of minutes left when you convert to hours (num % 60).

  • Use it whenever you have limited storage and you need to wrap around to the front if the value goes over the limit (num % limit).

3.1.1. groupwork Programming Challenge : Prime Numbers POGIL

We encourage you to do this activity as a POGIL (Process Oriented Guided Inquiry Learning) group activity or using Think-Pair-Share collaboration. POGIL groups are self-managed teams of 4 students where everyone has a POGIL role and works together to solve the problems, making sure that everyone in the team participates and learns.

In this activity, you will use boolean expressions to explore prime numbers. A prime number is an integer number that is only divisible by 1 and itself. For example, 3 is a prime number because it’s only divisible by 1 and 3 and no other numbers, but 4 is not a prime number because it’s divisible by 2 as well as 1 and 4.

Prime numbers are very useful in encryption algorithms because they can be used as keys for encoding and decoding. If you have the key, you can use it to divide a large number that represents something encrypted to decode it, but if you don’t have the key, it’s very hard to guess the factors of a large number to decode it. If you’re curious about this, watch this Numberphile video.

The following program checks if 5 is a prime number by seeing if it is divisible by the numbers 1 - 5. Run the code, and then answer the following questions.

  1. Is 5 a prime number?

  2. What boolean tests determine that a number is prime?

  3. Change the number to 6 and add more boolean expressions to determine if 6 is prime. Is 6 prime?

  4. Change the number to 7 and add more boolean expressions to determine if 7 is prime. Is 7 prime?

  5. If you changed the boolean expressions to use <= instead of ==, would the code still help you to find prime numbers? Why or why not? Experiment and find out.

  6. If you changed the boolean expressions to use >= instead of ==, would the code still help you to find prime numbers? Why or why not? Experiment and find out.

  7. Are all odd numbers prime? Can you find one that is not by using boolean expressions in the code below?

  8. Are all even numbers not prime? Can you find an even prime number?

3.1.2. 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.

You have attempted of activities on this page
Next Section - 3.2. if Statements and Control Flow