Conditionals
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?
ifstatements allow your program to execute different code blocks based on whether a condition istrueorfalse.- They use Boolean expressions (expressions that evaluate to
trueorfalse) to decide which path to take.
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:

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
ifblock (theprintstatement) only runs if the conditionstrawberries_in_fridge >= 7istrue. - If the condition is
false, the program skips the code inside theifblock 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:

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 >= 10isfalse, the code inside theelseblock executes. - Only one of the blocks (either the
ifor theelse) will execute in anif-elsestructure.
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.
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!
#
Key Takeaways
ifstatements make decisions based on Boolean conditions.elsestatements provide an alternative path when theifcondition isfalse.- Indentation is crucial in Python to define code blocks.
#đ Practice Questions
Practice Question
Multiple Choice Questions
- 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
ifcondition: 1 point - Correct output for passing grade: 1 point
- Correct
elsestatement: 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
ifandelsestatements 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
ifstatements 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! đĒ
Continue your learning journey

How are we doing?
Give us your feedback and let us know how we can improve





