AP Computer Science A · Unit 2 lecture · Part 1 of 12 · 8 min

2.1 Algorithms with selection and repetition · 2.2 Boolean expressionsQuestions with true or false answers

By the end: Trace and write code that makes decisions and repeats work: if statements, Boolean logic, while and for loops, the standard loop algorithms, String traversals and nested loops, and count how many times a statement runs.
Mr. Thompkins
Welcome to Unit 2, the biggest unit on the exam. In Unit 1 your programs ran every line, top to bottom, once. Now they get to choose and to repeat, and that's where programs start to feel smart.

Every algorithm is built from three building blocks. Sequencing runs steps in order. Selection chooses which steps to run, based on a true-or-false test: "if it's raining, take the umbrella." Repetition, also called iteration, repeats steps while a test stays true: "while there are dishes in the sink, wash one." Combine the three and you can describe any algorithm at all.

sequenceselectionrepetitionstep 1step 2step 3test?truefalseABnexttest?truebodyfalseafterUnit 1if statementsloops
The three building blocks drawn as flowcharts. Selection takes one of two paths. Repetition loops back to its test.
?Wait, what's a boolean again?
boolean is the primitive type with exactly two values, true and false (no quotes). You can store one in a variable, like boolean done = false;, and every test in an if statement or a loop must be a boolean expression.

The test is a Boolean expression: an expression whose value is true or false. The simplest ones use the relational operators, which compare two values: == (equal to), != (not equal to), <, >, <= and >=. Each comparison produces a boolean, which you can store, print or use as a test.

ExpressionMeaningWith x = 7, y = 10
x == yequal tofalse
x != ynot equal totrue
x < yless thantrue
x > ygreater thanfalse
x <= 7less than or equal totrue
y >= 11greater than or equal tofalse
The six relational operators. Note the spelling: == and != have two characters, and <= and >= put the < or > first.
?Wait, what's the difference between = and == again?
= stores a value in a variable (assignment). == compares two values and produces true or false. if (x = 5) doesn't even compile in Java, because x = 5 is an int, not a boolean.

Use == and != for primitive values: ints, booleans, and with care, doubles. Because doubles can carry round-off error, 0.1 + 0.2 == 0.3 is false. Use == on objects only when you really mean "is this the very same object?", which is topic 2.6.

Pause and think
int a = 7; int b = 2; Is a / b * b == a true or false?
I thought about it. Show me
False. a / b is 7 / 2, which is 3 with ints. 3 * 2 is 6, and 6 == 7 is false. That's actually a handy test: n / d * d == n is true exactly when d divides n with no remainder, just like n % d == 0.
Evaluate the tests

With int a = 7 and int b = 2, evaluate each expression.

(a) a / b == 3
(b) a % b != 0
(c) a / 2.0 >= 3.5
(d) (a + b) % 3 == 0
  1. (a) a / b is 7 / 2, which is 3 (integer division). 3 == 3 is true.
    Why?
    Arithmetic happens before comparison. Work out each side first, then compare.
  2. (b) a % b is 7 % 2, which is 1. 1 != 0 is true. (This is the test for "a is odd".)
  3. (c) a / 2.0 is 3.5, a double. 3.5 >= 3.5 is true, because >= includes equal.
  4. (d) a + b is 9, and 9 % 3 is 0. 0 == 0 is true. (This is the test for "a + b is a multiple of 3".)

Answer: All four are true.

Stuck? Get a hint
  1. Do the arithmetic on each side first. Remember 7 / 2 is 3 with ints.
  2. % gives the remainder: 7 % 2 is 1, 9 % 3 is 0.
  3. >= is true when the two sides are equal, too.
Check yourself
Which expression is true when the int n is a multiple of 5?
  1. An / 5 == 0
  2. Bn % 5 == 0
  3. Cn % 5 == 5
  4. Dn / 5 == 1
Show the answer

B. A multiple of 5 leaves no remainder when divided by 5, so n % 5 == 0. Choice A is the trap: n / 5 == 0 is true for 0 through 4, not for multiples of 5. The remainder of a division by 5 is never 5, so choice C is never true.

  • Three building blocks: sequencing, selection (choose), repetition (repeat).
  • Relational operators ==, !=, <, >, <=, >= produce a boolean.
  • n % d == 0 tests divisibility. n % 2 == 0 tests even.
  • Be careful with == on doubles (round-off) and on objects (that's "same object", topic 2.6).