Welcome to the World of Decisions: Conditionals

In our previous chapters, we looked at how programs follow a sequence of instructions and how they use variables to store information. But imagine if a program could only do the exact same thing every single time! It wouldn't be very "smart," would it? Conditionals are the secret ingredient that allows a program to make decisions. They let our code say, "If this is true, do one thing; otherwise, do something else."

Whether it's a video game checking if your health is \( 0 \) or a weather app deciding whether to show a sun or a rain cloud, conditionals are at the heart of the logic. Don't worry if this seems a bit abstract at first—we'll break it down step-by-step!

3.6: Simple Selection (IF and IF/ELSE)

In the AP CSP curriculum, selection is the formal term for using a conditional to determine which part of an algorithm to execute. We use the Boolean expressions we learned in the previous chapter (expressions that result in either \( true \) or \( false \)) to make these choices.

The Basic IF Statement

The simplest form of a conditional is the IF statement. It acts like a gatekeeper. If the condition is \( true \), the code inside the curly braces \( \{ \} \) runs. If the condition is \( false \), the program simply skips over that block and moves on to the next line of code.

Exam Reference Sheet Notation:
IF(condition)
{
    <block of statements>
}

Example: Imagine a game where you get a bonus if your score is over \( 100 \).
IF(score > 100)
{
    bonusPoints <- bonusPoints + 10
}

If \( score \) is \( 105 \), the bonus is added. If \( score \) is \( 90 \), the program ignores the bonus and keeps going.

The IF/ELSE Statement

Sometimes, we want the program to do one thing if a condition is \( true \) and a different thing if it is \( false \). This is where the ELSE statement comes in. It provides an alternative path.

Exam Reference Sheet Notation:
IF(condition)
{
    <first block of statements>
}
ELSE
{
    <second block of statements>
}

Real-World Analogy: Think of a fork in the road. If the sign says "Left for Pizza," you go left. Else (otherwise), you go right for Tacos. You have to pick one path; you can't go both ways at the same time!

Key Takeaway:

In an IF/ELSE structure, exactly one of the two blocks will execute. The program will never run both, and it will never skip both.


3.7: Nested Conditionals

Life is often more complicated than a simple "yes" or "no." Sometimes, a decision depends on another decision. In programming, we call this Nested Conditionals—where an \( IF \) statement is placed inside another \( IF \) or \( ELSE \) block.

Example: Choosing what to wear based on the weather.
IF(isRaining = true)
{
    IF(isCold = true)
    {
        DISPLAY("Wear a heavy raincoat")
    }
    ELSE
    {
        DISPLAY("Wear a light poncho")
    }
}
ELSE
{
    DISPLAY("No jacket needed")
}

Hand-Tracing Tip: When dealing with nested conditionals, work from the outside in! First, check the outer \( IF \) condition. If that is \( false \), you can skip the entire nested section inside it.

Key Takeaway:

Nested conditionals allow for more complex algorithms by checking multiple layers of conditions.


Visualizing Logic: Flowcharts

On the AP Exam, you might see algorithms represented as flowcharts. It is very important to recognize the specific shapes used for conditionals:

  • Ovals: Represent the start or end of the algorithm.
  • Rectangles: Represent a step or a result (like a calculation).
  • Diamonds: Represent a Conditional/Decision step.

Inside the diamond, you will see a question or a Boolean expression. Two arrows will point out of the diamond: one labeled Yes/True and one labeled No/False. Follow the arrow that matches the result of your condition to see what the program does next.


Common Mistakes to Avoid

1. Confusing Assignment and Comparison:
In the AP Exam notation, \( a <- b \) means you are assigning the value of \( b \) to \( a \). However, inside a conditional, you will use \( a = b \) to compare them. Don't mix them up!

2. Misinterpreting Boolean Operators:
Remember your logic gates from the "Boolean Expressions" chapter:
- \( NOT \) flips the value (\( true \) becomes \( false \)).
- \( AND \) requires both sides to be \( true \).
- \( OR \) requires at least one side to be \( true \).

3. Forgetting the "Side Effects":
A "side effect" is when a program statement changes the value of a variable. Pay close attention to any assignment statements (\( <- \)) inside the conditional blocks. If the condition is \( false \), those variables will not change.


Quick Review: Check Your Understanding

Did you know? Computers evaluate conditionals using 0s and 1s at the lowest level, but as programmers, we use Selection to make that logic easy for humans to read and design.

Summary Checklist:
  • Can you identify an \( IF \) statement and determine if its code will run?
  • Can you explain the difference between a simple \( IF \) and an \( IF/ELSE \) structure?
  • Do you know that a Diamond shape in a flowchart represents a decision?
  • Can you "trace" a nested conditional by checking the outer condition first?

Keep practicing! Conditionals are like the steering wheel of your program—once you master them, you can guide your code anywhere you want it to go!