📚

All Subjects

 > 

⌨️ 

AP Comp Sci P

 > 

📱

Big Idea 3: Algorithms and Programming

Conditionals

1 min read•november 16, 2020

minnachow

Minna Chow


AP Computer Science Principles ⌨️

Bookmarked 2.3k • 69 resources
See Units

Earlier, we introduced the concept of sequencing, selection and iteration. The selection process primarily takes the form of conditional statements known as if statements. Certain requirements, or conditions, need to be met in order for selection statements to run.
If statements execute different segments of code based on the result of a Boolean expression. Here's the format of a basic if statement in Pseudocode...

Pseudocode Example

https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-cEzMARadr3gv.png?alt=media&token=6a799bf7-b62f-4b6e-b94d-4785df6e4af9
And Python!
strawberries_in_fridge = 7 if strawberries_in_fridge >= 7: print ("You can make strawberry shortcake!")
The code in the if statement is executed only if the condition is met. In this case, the condition is the boolean expression strawberries_in_fridge ≥ 7. The condition is true, so the code inside the if statement (the print statement) will execute.
If strawberries_in_fridge was less than seven in this case, the program would just skip over the code inside the statement and continue to the next part of the program.
However, if statements can also have else statements attached to them that specify what would happen if the condition wasn't met. Here they are in Pseudocode...
https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-3eGPDZtgSI16.png?alt=media&token=3558d7d8-a142-413c-8564-8c84c343a151
...and Python again.
strawberries_in_fridge = 7 if strawberries_in_fridge >= 10: print ("You can make strawberry shortcake!") else: print ("Sorry, no shortcake for you!")
Else statements will only run when the condition attached to an if statement comes back false. In this case, because the if statement isn't true, the program will run the else statement instead.

Was this guide helpful?

Join us on Discord
Thousands of students are studying with us for the AP Computer Science Principles exam.
join now
Hours Logo
Studying with Hours = the ultimate focus mode
Start a free study session
🔍 Are you ready for college apps?
Take this quiz and find out!
Start Quiz
Browse Study Guides By Unit
🕹Big Idea 1: Creative Development
⚙️Big Idea 2: Data
📱Big Idea 3: Algorithms and Programming
🖥Big Idea 4: Computer Systems and Networks
⌨️Big Idea 5: Impact of Computing
✏️Blogs
📝Exam Prep
Join us on Discord
Thousands of students are studying with us for the AP Computer Science Principles exam.
join now
💪🏽 Are you ready for the AP CSP exam?
Take this quiz for a progress check on what you’ve learned this year and get a personalized study plan to grab that 5!
START QUIZ