zuai-logo

Conditionals

David Foster

David Foster

5 min read

Listen to this study note

Study Guide Overview

This study guide covers control flow in programming, focusing on selection with if statements, else statements, and Boolean expressions. It explains the structure and logic of these statements in pseudocode and Python, provides practice questions (MCQs and FRQs) involving conditional logic, and offers final exam tips emphasizing the importance of these concepts, indentation, and combining them with other programming elements like loops and data structures.

AP Computer Science Principles: The Night Before

Hey there! Let's get you totally prepped for the AP CSP exam. We're going to make sure everything clicks, so you can walk in feeling confident. Let's dive in!

🚀 Control Flow: Sequencing, Selection, and Iteration

Selection: Making Decisions with if Statements

We're diving into selection, which is all about making choices in your code using conditional statements (aka if statements). Think of it like a choose-your-own-adventure book, but for computers! đŸ•šī¸

What are if Statements?

  • if statements allow your program to execute different code blocks based on whether a condition is true or false.
  • They use Boolean expressions (expressions that evaluate to true or false) to decide which path to take.
Key Concept

if statements are fundamental for creating dynamic and responsive programs. They're how your code makes decisions!

Basic if Statement Structure

Here's how a basic if statement looks in pseudocode:

If statement pseudocode diagram

And here's the same concept in Python:

python
strawberries_in_fridge = 7

if strawberries_in_fridge >= 7:
    print("You can make strawberry shortcake!")
  • The code inside the if block (the print statement) only runs if the condition strawberries_in_fridge >= 7 is true.
  • If the condition is false, the program skips the code inside the if block and moves on.

else Statements: Handling the "What If Not?"

else statements are like the backup plan for if statements. They specify what should happen if the if condition is false. 💡

if-else Structure

Here's the pseudocode for an if-else statement:

If-else statement pseudocode diagram

And here's the Python version:

python
strawberries_in_fridge = 7

if strawberries_in_fridge >= 10:
    print("You can make strawberry shortcake!")
else:
    print("Sorry, no shortcake for you!")
  • If strawberries_in_fridge >= 10 is false, the code inside the else block executes.
  • Only one of the blocks (either the if or the else) will execute in an if-else structure.
Memory Aid

Think of if-else like a fork in the road: you can only go down one path based on the condition. If the condition is true, you go down the 'if' path; otherwise, you take the 'else' path.

Exam Tip

Be careful with indentation in Python. It's how the interpreter knows which code belongs to the if or else blocks. Incorrect indentation can lead to errors!

Quick Fact

Key Takeaways

  • if statements make decisions based on Boolean conditions.
  • else statements provide an alternative path when the if condition is false.
  • Indentation is crucial in Python to define code blocks.

📝 Practice Questions

Practice Question

Multiple Choice Questions

  1. What is the output of the following code?
python
x = 15
if x > 10:
    print("A")
else:
    print("B")
(A) A
(B) B
(C) A B
(D) No output

2. Which of the following is NOT a valid relational operator used in a conditional statement?

(A) `==`
(B) `!=`
(C) `=`
(D) `>=`

Free Response Question

Write a function check_grade that takes a numerical score as input and prints "Pass" if the score is 60 or greater, and prints "Fail" otherwise.

Scoring Breakdown:

  • Correct function definition: 1 point
  • Correct if condition: 1 point
  • Correct output for passing grade: 1 point
  • Correct else statement: 1 point
  • Correct output for failing grade: 1 point

Example Solution:

python
def check_grade(score):
    if score >= 60:
        print("Pass")
    else:
        print("Fail")

Final Exam Focus

  • if and else statements are essential: Expect them in both MCQs and FRQs.
  • Boolean expressions: Make sure you understand how to evaluate them.
  • Indentation: Pay close attention to indentation in Python code.
  • Combine concepts: AP questions often combine multiple concepts, so be ready to use if statements within loops or with other data structures.

Last-Minute Tips

  • Time Management: Don't spend too long on a single question. Move on and come back if you have time.
  • Read Carefully: Pay close attention to the wording of each question.
  • Practice: Do a few practice questions to get in the right mindset.

You've got this! Go in there and show them what you know! đŸ’Ē

Question 1 of 9

What will the following code print? 🍓

python
x = 10
if x > 5:
    print("Success!")

Success!

Nothing

Error

x