Staging: a draft, not the live site
✕ Close

Week 13 · 13.4 · Thu, Oct 29 · We Do + Try Its

True or False: Comparisons and Logical Operators

This section shows the order of the steps, then the four kinds of steps on today's bench: working out a comparison, using a remainder to ask a question, joining answers with and, or and not, and tracing boxes that hold true or false.

The Order of the Steps

Yesterday's long division wrote an answer with a little r, such as 17 ÷ 5 = 3 r 2. The steps language splits that answer into two signs. The sign ÷ gives the whole part, and rem gives the part after the r.

117 ÷ 5   = 3 r 2     long division
217 ÷ 5   = 3         ÷ keeps the whole part
317 rem 5 = 2         rem keeps the r

The sign rem works at the same level as times and divide. A line with several signs is worked in this order:

11st   ( )                    inside the parentheses first
22nd   *   ÷   rem            left to right
33rd   +   −                  left to right
44th   <  >  ≤  ≥  =  ≠       comparisons come last

This is the order of PEMDAS, with rem joining multiply and divide. Signs on the same level are worked from left to right.

Example 1. Work out 3 + 20 rem 6 * 2.

Show the solution
13 + 20 rem 6 * 2
2= 3 + 2 * 2      rem first: it is the leftmost sign on the 2nd level
3= 3 + 4          then *
4= 7              + comes after the 2nd level

Example 2. Work out 20 ÷ 4 rem 3.

Show the solution
120 ÷ 4 rem 3
2= 5 rem 3        ÷ and rem share a level, so go left to right
3= 2

Example 3. Work out (3 + 20) rem 6.

Show the solution
1(3 + 20) rem 6
2= 23 rem 6       parentheses first
3= 5

In a comparison such as 14 rem 2 = 0, the rem is worked before the = sign, because comparisons come last.

Try It on your own

Work each one in your notebook first, then check it.

Try It 1. Work out 5 + 9 rem 4.

Try It 2. Work out 17 rem 5 * 3.

Try It 3. In 4 + 10 rem 3 = 5, which sign is worked last?

Comparisons

A comparison checks how two values relate, and its answer is always true or false. The steps language uses six signs. In November, the class will see Java write four of them with two characters, as shown below.

1<    less than                    < in Java
2>    greater than                 > in Java
3≤    less than or equal to        <= in Java
4≥    greater than or equal to     >= in Java
5=    equal to                     == in Java
6≠    not equal to                 != in Java

How to work out a comparison:

  1. Work out each side until it is one number.
  2. Read the sign in words, such as "is 8 less than 5".
  3. Answer true or false.

Example 1. Work out 8 < 5 and 8 > 5.

18 < 5     is 8 less than 5?        false
28 > 5     is 8 greater than 5?     true

Example 2. Work out 4 < 4 and 4 ≤ 4.

14 < 4     is 4 less than 4?                false
24 ≤ 4     is 4 less than or equal to 4?    true
Show the solution

The two signs differ only when the sides are equal. The sign ≤ counts the equal case, and the sign < does not.

Example 3. Work out 3 * 4 ≥ 2 + 10.

Show the solution
13 * 4 ≥ 2 + 10
2= 12 ≥ 12      work each side first
3= true         12 is greater than or equal to 12

The arithmetic on each side is finished before the comparison is answered.

Try It on your own

Work each one in your notebook first, then check it.

Try It 4. Is 9 ≥ 9 true or false?

Try It 5. Work out each side first. Is 2 * 6 < 3 + 9 true or false?

Try It 6. Which comparison is true?

Remainders That Ask a Question

The remainder from the day before can ask a question. When n rem 2 is 0, the number n divides by 2 with nothing left over, so n is even. When n rem 2 is not 0, n is odd.

Example 4. Decide whether 14 and 9 are even.

Show the solution
114 rem 2 = 0     14 rem 2 is 0, so    true
29 rem 2 = 0      9 rem 2 is 1, so     false

Example 5. Decide whether 9 is odd.

Show the solution
19 rem 2 ≠ 0     9 rem 2 is 1, and 1 is not 0, so    true

The same idea works for any divisor. The comparison n rem 5 = 0 is true when n is a multiple of 5, such as 35, and false for a number such as 37.

Try It on your own

Work each one in your notebook first, then check it.

Try It 7. Is 38 rem 2 = 0 true or false?

Try It 8. For which number is n rem 2 ≠ 0 true?

Try It 9. Write a comparison that is true exactly when n is a multiple of 3. Then work it out for n = 21 and n = 22.

Check your answer

n rem 3 = 0. For 21, 21 rem 3 is 0, so the comparison is true. For 22, 22 rem 3 is 1, so it is false.

And, Or and Not

A logical operator joins two answers or flips one. The operator and is true only when both sides are true. The operator or is true when at least one side is true. The operator not turns true into false and false into true.

Example 6. A number is between 10 and 20, inclusive, when it is at least 10 and at most 20. Check 20 and 21.

Show the solution
120 ≥ 10 and 20 ≤ 20     true and true      true
221 ≥ 10 and 21 ≤ 20     true and false     false

Inclusive means the end numbers count, so the rule uses ≥ and ≤. A rule that leaves out the ends uses > and <.

Example 7. A ticket is free when a person is under 5 or at least 65. Check a person who is 70.

170 < 5 or 70 ≥ 65     false or true     true

Example 8. Work out not (3 < 2).

1not (3 < 2)
2= not false     3 is not less than 2
3= true
Show the solution

The part in parentheses is worked first, and then not flips it.

Try It on your own

Work each one in your notebook first, then check it.

Try It 10. Work it out. Is 15 > 10 and 15 < 12 true or false?

Try It 11. Which rule is true exactly when n is between 1 and 10, inclusive?

Try It 12. Work it out. What is not (5 ≤ 5)?

Boxes That Hold True or False

On the bench, a box can hold true or false in the same way that it holds a number. A line that stores a comparison works the comparison out first and stores only its answer. A trace table records each box after every line runs, and shown records what a show line puts on the screen.

Example 9. Trace these lines.

1n ← 27
2odd ← n rem 2 ≠ 0
3big ← n > 30
4show odd and big
Show the solution
line   n    odd    big     shown
1      27
2      27   true
3      27   true   false
4      27   true   false   false

Line 2 stores true, because 27 rem 2 is 1. Line 3 stores false, because 27 is not greater than 30. Line 4 shows false, because and needs both sides to be true.

The bench catches three common mistakes, and each one changes the answer. The first is mixing up < and ≤ when the two sides are equal. The second is reading or as if it needed both sides to be true. The third is storing a number in a box when the line stores the answer to a comparison.

Sources: Oracle, The Java Tutorials: Equality, Relational, and Conditional Operators and Primitive Data Types; course repository, Bench Basics B09 job data (2026).

Try It on your own

Work each one in your notebook first, then check it.

Try It 13. After line 3, what do the boxes hold?

1k ← 16
2even ← k rem 2 ≠ 1
3small ← k < 10

Try It 14. Trace these lines the way Example 9 does. What does line 4 show?

1t ← 64
2hot ← t ≥ 80
3cold ← t < 50
4show not hot and not cold
Check your answer

Line 2 stores false, because 64 is not at least 80. Line 3 stores false, because 64 is not less than 50. Line 4 works not hot, which is true, and not cold, which is true, and true and true is true. Line 4 shows true.