Selection (if/else) and logic - Computer Science A AP Study Notes

Overview
# Selection (if/else) and Logic - Summary This lesson covers conditional statements and Boolean logic, essential for controlling program flow in Java. Students learn to implement if, if-else, and nested conditionals, apply logical operators (&&, ||, !), and understand short-circuit evaluation and De Morgan's Laws. These concepts are fundamental for AP Computer Science A, appearing extensively in both multiple-choice questions and free-response problems requiring decision-making algorithms and complex conditional expressions.
Core Concepts & Theory
Selection statements allow programs to make decisions and execute different code paths based on conditions. This is fundamental to creating intelligent, responsive software.
Key Terms:
Conditional statements (if/else) evaluate Boolean expressions to determine program flow. An if statement executes code only when a condition is true. An else clause provides an alternative path when the condition is false. else if (or elif) allows testing multiple conditions sequentially.
Boolean expressions return true or false values. They use relational operators: == (equal to), != (not equal), > (greater than), < (less than), >= (greater than or equal), <= (less than or equal).
Logical operators combine multiple conditions:
- AND (
&&orand): Both conditions must be true - OR (
||oror): At least one condition must be true - NOT (
!ornot): Inverts the Boolean value
Truth Tables (essential for Cambridge exams):
| A | B | A AND B | A OR B | |---|---|---------|--------| | T | T | T | T | | T | F | F | T | | F | T | F | T | | F | F | F | F |
Nested selection places if statements inside others, enabling complex decision-making. The general syntax follows:
if (condition1)
// code block
else if (condition2)
// code block
else
// default code
Order of evaluation matters: logical operators follow precedence (NOT > AND > OR). Use parentheses to control evaluation order explicitly.
Cambridge Note: Examiners expect precise use of terminology. "Selection" is preferred over "branching" or "decision-making" in formal answers.
Detailed Explanation with Real-World Examples
Selection statements mirror everyday decision-making. Think of a traffic light system: IF light is red THEN stop, ELSE IF light is amber THEN prepare to stop, ELSE (green) proceed. This nested logic ensures only one action occurs.
Real-World Application 1: Authentication Systems
When you log into a website, the system uses selection:
if (username correct AND password correct)
grant access
else if (username correct AND password incorrect)
display "wrong password"
else
display "account not found"
The AND operator ensures both credentials validate. Using OR would create a security vulnerability—imagine granting access with just a correct username!
Real-World Application 2: E-commerce Discount Logic
Online stores calculate prices using nested selection:
if (customer is VIP)
discount = 20%
else if (order total > £100)
discount = 15%
else if (first purchase)
discount = 10%
else
discount = 0%
Notice the order matters: VIP status checks first, preventing customers getting lesser discounts.
Analogy: A Restaurant Menu Decision Tree
Selecting a meal follows logical steps: IF vegetarian THEN show veggie options, ELSE IF allergic to nuts THEN filter nut dishes, ELSE show full menu. Each condition narrows choices systematically.
Short-Circuit Evaluation optimizes performance: In A AND B, if A is false, B isn't checked (result must be false). In A OR B, if A is true, B isn't evaluated. This prevents errors: if (divisor != 0 AND number/divisor > 5) safely checks division by placing the guard condition first.
Memory Aid (MNEMONIC): "AND needs ALL, OR needs ONE" helps remember logical operator requirements.
Worked Examples & Step-by-Step Solutions
**Example 1: Grade Boundary Calculator (6 marks)** *Question:* Write pseudocode that inputs a percentage mark and outputs a grade. A (≥70), B (60-69), C (50-59), D (40-49), F (<40). **Solution:** ``` INPUT mark IF mark >= 70 THEN OUTPUT "Grade A" ELSE IF mark >= 60 THEN OUTPUT "Grade B" EL...
Unlock 3 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