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?
if
statements allow your program to execute different code blocks based on whether a condition istrue
orfalse
.- They use Boolean expressions (expressions that evaluate to
true
orfalse
) 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
if
block (theprint
statement) only runs if the conditionstrawberries_in_fridge >= 7
istrue
. - If the condition is
false
, the program skips the code inside theif
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:
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
isfalse
, the code inside theelse
block executes. - Only one of the blocks (either the
if
or theelse
) will execute in anif-else
structure.
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
if
statements make decisions based on Boolean conditions.else
statements provide an alternative path when theif
condition 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
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
andelse
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! đĒ
Explore more resources

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