Back to homeAP® Computer Science Principles · AP Computer Science Principles · Change subject
Start for free
AP CSP · AP® Computer Science Principles
AP Computer Science Principles: Data, Algorithms, and Impact · Big Idea 3: Algorithms and Programming

Boolean Expressions, Conditionals, and Iteration

Your school's roller coaster trip has a sign: "You must be at least 48 inches tall." A worker checks each rider: yes or no. Then the next rider, and the next, until the line is empty. That is a computer program in real life. A question with a yes/no answer, a choice based on it, and a check that repeats. In this page you will learn all three, and at the end you will return to the roller coaster.

Boolean expressions

A Boolean expression is an expression whose value is either true or false. Nothing else, no "maybe."

You build them with relational operators, which compare two values:

Operator Meaning Example (x ← 7) Value
= equal to x = 7 true
≠ not equal to x ≠ 7 false
> greater than x > 5 true
< less than x < 5 false
≥ greater than or equal to x ≥ 7 true
≤ less than or equal to x ≤ 6 false

Notice that in AP pseudocode = means "is equal to?" (a question). Assignment, which stores a value, uses the arrow ←.

You can combine Boolean values with logical operators:

  • NOT a flips the value: true becomes false, false becomes true.

  • a AND b is true only if both are true.

  • a OR b is true if at least one is true.

Some expressions are logically equivalent, meaning they always have the same value. Two useful ones: NOT (a AND b) is the same as (NOT a) OR (NOT b), and NOT (a OR b) is the same as (NOT a) AND (NOT b). Notice that negating flips each part and also swaps AND with OR.

Truth table for AND and OR with two Boolean inputs A and B A B A AND B A OR B truetruetruetrue truefalsefalsetrue falsetruefalsetrue falsefalsefalsefalse
Practice★☆☆Not started
Let A be true and B be false. Evaluate each expression: a) A AND B b) A OR B c) NOT B d) NOT (A AND B)

Let A be true and B be false. Evaluate each expression:

  1. a) A AND B
  1. b) A OR B
  1. c) NOT B
  1. d) NOT (A AND B)
Solve
Practice★☆☆Not started
Let x ← 7. Evaluate each as true or false: a) (x > 5) AND (x < 10) b) (x < 3) OR (x MOD 2 = 1) c) NOT (x = 7)

Let x ← 7. Evaluate each as true or false:

  1. a) (x > 5) AND (x < 10)
  1. b) (x < 3) OR (x MOD 2 = 1)
  1. c) NOT (x = 7)
Solve
Practice★★☆Not started
A roller coaster rule says: a rider may board if height ≥ 48 AND (age ≥ 12 OR withAdult), where height is in inches and withAdult is true or false. Evaluate the rule for…

A roller coaster rule says: a rider may board if height ≥ 48 AND (age ≥ 12 OR withAdult), where height is in inches and withAdult is true or false.

Evaluate the rule for each rider:

Rider Height (in.) Age With adult
Chloe 50 10 false
Malik 52 13 false
Camila 45 15 true

Give true or false for each, and then write a Boolean expression that says when a rider is not allowed to board.

Solve

Selection with IF and ELSE

Selection lets a program choose which code runs, depending on a Boolean expression.

IF (condition) {

   block A

}

ELSE {

   block B

}

If the condition is true, block A runs. Otherwise, block B runs. The ELSE part is optional. Without it, nothing happens when the condition is false.

You can put an IF inside another one. This is a nested conditional. Nesting is how you choose among more than two outcomes.

Flowchart of nested conditionals that display A, B or C depending on score score ≥ 90 ? true DISPLAY("A") false score ≥ 80 ? true DISPLAY("B") false DISPLAY("C")
Practice★★☆Not started
a) What is displayed for score equal to 95, 85, 80, and 79? b) If the first condition were changed to score > 90, which score value would give a different result? What…
IF (score ≥ 90)

{

  DISPLAY("A")

}

ELSE

{

  IF (score ≥ 80)

  {

    DISPLAY("B")

  }

  ELSE

  {

    DISPLAY("C")

  }

}
  1. a) What is displayed for score equal to 95, 85, 80, and 79?
  1. b) If the first condition were changed to score > 90, which score value would give a different result? What would be displayed?
Solve

Iteration with REPEAT

Iteration means repeating a block of code. AP pseudocode gives you two main loops:

  • REPEAT n TIMES { … } runs the body exactly n times.

  • REPEAT UNTIL (condition) { … } checks the condition before each pass. If it is false, the body runs again. When it becomes true, the loop stops. If the condition is true from the start, the body never runs.

FOR EACH item IN aList { … } runs the body once for every element of a list, with item holding the current element. For example, if aList is [4, 7, 9], then FOR EACH item IN aList { DISPLAY(item * 2) } displays 8, 14, and 18. You will use it a lot with lists.

The best way to understand a loop is to hand trace it: make a table and update each variable step by step.

A loop that never stops is an infinite loop. It happens when the condition can never become true. For example:

n ← 1

REPEAT UNTIL (n < 0) {

   n ← n * 2

}

Here n only grows, so n < 0 is never true. Two common causes of infinite loops: forgetting to update the variable that the condition checks, and using an update that "jumps over" the stopping value.

Number line showing that stepping by 2 from 1 jumps over the stopping value 4 123456 stop value 4 i goes 1, 3, 5, … and never equals 4
Practice★☆☆Not started
a) How many times is "Go" displayed by REPEAT 4 TIMES { DISPLAY("Go") }? b) What is the final value of n?
  1. a) How many times is "Go" displayed by REPEAT 4 TIMES { DISPLAY("Go") }?
  1. b) What is the final value of n?
n ← 0

REPEAT 5 TIMES

{

  n ← n + 2

}
Solve
Practice★★☆Not started
a) What is displayed? b) How many times does the loop body run? c) What would happen if the condition were n < 0 instead?
n ← 1

REPEAT UNTIL (n > 50)

{

  n ← n * 2

}

DISPLAY(n)
  1. a) What is displayed?
  1. b) How many times does the loop body run?
  1. c) What would happen if the condition were n < 0 instead?
Solve
Practice★★★Not started
Three programmers wrote loops that are supposed to display 1, 2, 3, 4, 5. Version A: Version B: Version C: a) For each version, give the output or say that it never…

Three programmers wrote loops that are supposed to display 1, 2, 3, 4, 5.

Version A:

i ← 1

REPEAT UNTIL (i = 5)

{

  DISPLAY(i)

  i ← i + 1

}

Version B:

i ← 1

REPEAT UNTIL (i = 6)

{

  DISPLAY(i)

  i ← i + 1

}

Version C:

i ← 1

REPEAT UNTIL (i = 4)

{

  DISPLAY(i)

  i ← i + 2

}
  1. a) For each version, give the output or say that it never stops.
  1. b) Explain what is wrong with Version A and how to fix it.
  1. c) Explain why Version C is an infinite loop, and describe a condition that would make it stop safely.
Solve

Putting it together

Real programs combine all three ideas: a loop repeats, and inside it a conditional decides what to do with each item. This is the pattern behind counting, adding up only some values, and searching.

Practice★★★Not started
a) What is displayed, and how many times does the loop body run? b) What would happen if the line i ← i + 1 were removed? Explain. c) Describe the change needed so that…
i ← 1

total ← 0

REPEAT UNTIL (i > 10)

{

  IF (i MOD 3 = 0)

  {

    total ← total + i

  }

  i ← i + 1

}

DISPLAY(total)
  1. a) What is displayed, and how many times does the loop body run?
  1. b) What would happen if the line i ← i + 1 were removed? Explain.
  1. c) Describe the change needed so that the program adds up the even numbers from 1 to 10 instead.
Solve

Summary

See all the exercises in this section