AP Computer Science Principles · Study guide

Conditionals

AP® Computer Science Principles · Big Idea 3 · Algorithms and Programming

Conditionals

How a program chooses which steps to run: selection in plain words, IF and IF-ELSE statements, and how to trace them with more than one input.

1Selection

Selection decides which parts of an algorithm run, based on whether a condition is true or false. You use it every day without code:

Check the forecast. If rain is forecast, pack an umbrella; otherwise, pack sunglasses. Leave for school.

Three things to notice. The condition, “rain is forecast”, is a yes-or-no question. Only one of the two middle steps happens on any morning. And the steps before and after the choice, checking the forecast and leaving, happen every time.

Selection asks its question once, each time the algorithm reaches it. Asking the same question over and over until something changes is iteration, a different building block.

2IF

Conditional statements, also called if-statements, change the normal top-to-bottom order of a program by running different statements depending on a Boolean expression.

Reference language
IF (condition)
{
  <block of statements>
}

The block runs if condition is true. If it is false, no action is taken and the program continues after the closing brace.

price ← 40
IF (items > 10)
{
  price ← price - 5
}
DISPLAY(price)

With items equal to 12, the condition is true and this displays 35. With 10, 10 > 10 is false, so the price stays at 40.

Figure 1. With IF alone, a false condition means no action. With IF and ELSE, one of the two blocks always runs, never both.

3IF with ELSE

Reference language
IF (condition)
{
  <first block of statements>
}
ELSE
{
  <second block of statements>
}

The first block runs if condition is true; otherwise the second block runs. One block always runs, and never both.

IF (items > 10)
{
  shipping ← 0
}
ELSE
{
  shipping ← 7
}
DISPLAY(shipping)
itemsitems > 10Block that runsDisplays
12truefirst0
10falseELSE7
3falseELSE7

Trace a conditional by writing the condition, its value, and then only the statements of the block that runs. Test the boundary: 10 items is not more than 10, so it pays shipping. If the condition were items ≥ 10, 10 items would ship free.

The key idea

IF runs its block only when the condition is true and otherwise does nothing. IF with ELSE runs the first block when the condition is true and the second block when it is false, so one of them always runs.

4Two IFs are not one IF-ELSE

Two separate IF statements each test their own condition, so both blocks can run, or neither. An IF-ELSE always runs one block.

Two separate IFs
grade ← "none"
IF (score ≥ 50)
{
  grade ← "pass"
}
IF (score ≥ 80)
{
  grade ← "merit"
}
DISPLAY(grade)
One IF-ELSE
IF (score ≥ 80)
{
  grade ← "merit"
}
ELSE
{
  grade ← "pass"
}
DISPLAY(grade)
scoreTwo IFs displayIF-ELSE displays
90meritmerit
60passpass
20nonepass

At 90, both IFs run in the first version; the second one overwrites pass with merit, so the results match. At 20 they differ: neither IF runs in the first version, but the ELSE in the second version catches every score below 80, including scores that should not pass. To compare two segments, test a value both conditions accept and a value only the ELSE would catch.

5Worked examples

Worked Example 1 Trace

Even or odd

What is displayed when n is 7?

IF (n MOD 2 = 0)
{
  DISPLAY("even")
}
ELSE
{
  DISPLAY("odd")
}
DISPLAY("done")
  1. 1
    7 MOD 2 is 1, so 7 MOD 2 = 0 is false.
  2. 2
    The ELSE block runs and displays odd.
  3. 3
    DISPLAY("done") is after the IF-ELSE, so it runs either way.
Answer: odd then done
Worked Example 2 Work backward

When does it display B?

For which whole numbers x from 0 to 12 does this display B?

result ← "A"
IF (x < 4 OR x > 9)
{
  result ← "B"
}
DISPLAY(result)
  1. 1
    It displays B only when the IF block runs, which needs x < 4 OR x > 9 to be true.
  2. 2
    Test the edges:
    xDisplays
    3B
    4A
    9A
    10B
Answer: x = 0, 1, 2, 3, 10, 11, 12.
Worked Example 3 Write a conditional

Free shipping

Orders of $35 or more ship free; smaller orders pay a $6 fee. Write a segment that displays the amount charged, and test it.

  1. 1
    “35 or more” is total ≥ 35. Every order either qualifies or pays, so use IF-ELSE.
    IF (total ≥ 35)
    {
      fee ← 0
    }
    ELSE
    {
      fee ← 6
    }
    DISPLAY(total + fee)
  2. 2
    Test the boundary and one value on each side:
    totalDisplays
    3535
    3440
    5050
Answer: $35 ships free; $34 is charged $40.
Worked Example 4 Compare segments

Same result?

Which of these always display the same value as Segment A?

Segment A

IF (x > 5)
{
  y ← 1
}
ELSE
{
  y ← 2
}
DISPLAY(y)

Segment B

y ← 2
IF (x > 5)
{
  y ← 1
}
DISPLAY(y)

Segment C

IF (x > 5)
{
  y ← 1
}
IF (x < 5)
{
  y ← 2
}
DISPLAY(y)
  1. 1
    Segment B sets y to 2 first and changes it to 1 only when x > 5, the same as A.
  2. 2
    Segment C has two IFs. When x is 5, neither condition is true, so y never gets a value and the program stops with an error.
  3. 3
    Test 4, 5, and 6:
    xABC
    4222
    522error: y has no value
    6111
Answer: Only Segment B.

6Common mistakes

Common Mistake 1 · Running both blocks

In an IF-ELSE, one block runs, never both. Cross out the block that does not run before you trace further.

Common Mistake 2 · Expecting an ELSE that is not there

An IF with no ELSE does nothing when its condition is false. Variables keep the values they had.

Common Mistake 3 · Treating two IFs like an IF-ELSE

Separate IFs are each tested, so both blocks can run, or neither.

Common Mistake 4 · Skipping the boundary value

items > 10 is false at 10. Always trace the value where the condition changes.

Explain a conditional like this

“When items is 10, the condition items > 10 is false, so the ELSE block runs and sets shipping to 7. The first block is skipped. The program displays 7.”

Give the condition’s value for the input, say which block runs and which is skipped, and give the result.