Selection (if/else) and logic - Computer Science A AP Study Notes
Overview
Imagine you're trying to decide what to wear based on the weather, or what to eat for lunch based on what's in the fridge. You make decisions all the time! In computer programming, computers also need to make decisions. This is where **Selection (if/else)** comes in. It's how we tell a computer to choose different paths or do different things based on certain conditions. This is super important because without it, computers would just do the same thing over and over, like a robot stuck on repeat. But with selection, they can be smart and react to different situations, just like you do. Think about a video game: your character moves left if you press the left arrow, and jumps if you press the spacebar. That's selection in action! We'll also look at **logic**, which is like the brainpower behind these decisions. It's how we figure out if a condition is true or false, so the computer knows which path to take. Mastering selection and logic is like giving your computer a brain to think and choose!
What Is This? (The Simple Version)
Think of it like a fork in the road or a choose-your-own-adventure book. When you come to a decision point, you look at a condition (like 'Is it sunny?') and then you choose which path to take based on whether that condition is true or false.
In programming, selection is how we tell the computer to make choices. The most common way to do this is with an if/else statement.
- An if statement says: "IF something is true, THEN do this specific action."
- An else statement says: "OTHERWISE (if that 'if' thing wasn't true), THEN do this other specific action instead."
It's like saying: "IF it's raining, THEN grab an umbrella. ELSE (if it's not raining), THEN leave the umbrella at home." Computers follow these instructions precisely.
Real-World Example
Let's imagine you're building a simple program for a traffic light. The traffic light needs to decide whether to show a green light or a red light.
-
Condition: Is there a car waiting at the intersection?
-
Decision Time!
- IF (Is there a car waiting? is TRUE) THEN:
- Change the light to green.
- Wait for a few seconds.
- Change the light to yellow, then red.
- ELSE (Is there a car waiting? is FALSE, meaning no car is there) THEN:
- Keep the light red (no need to change it if no one is waiting).
- IF (Is there a car waiting? is TRUE) THEN:
See? The traffic light makes a decision based on a condition, and then performs different actions depending on that decision. This is exactly how if/else works in code!
How It Works (Step by Step)
Let's break down how a computer actually processes an `if/else` decision. 1. **The computer reaches an `if` statement.** This is its signal that a decision needs to be made. 2. **It looks at the condition inside the `if` statement.** This condition is usually a question that can only be answered ...
Unlock 4 More Sections
Sign up free to access the complete notes, key concepts, and exam tips for this topic.
No credit card required ยท Free forever
Key Concepts
- Selection: The process where a computer chooses which code to run based on whether a condition is true or false.
- If Statement: A programming structure that executes a block of code only if a specified condition is true.
- Else Statement: A programming structure that executes a block of code if the condition in the preceding 'if' statement was false.
- Condition: A statement or expression that evaluates to either true or false, used to make decisions in selection structures.
- +6 more (sign up to view)
Exam Tips
- โAlways use `==` for comparison and `=` for assignment. This is a common source of errors on the exam.
- โPay close attention to curly braces `{}`! They define the blocks of code that belong to an `if`, `else`, or `else if` statement. Missing or extra braces can completely change how your code runs.
- +3 more tips (sign up)
More Computer Science A Notes