AP CSA Unit 2.1: Selection and If Statements Practice
Share
Practice Question
int score = 85;
int bonus = 0;
if (score >= 80) {
bonus = 10;
}
score = score + bonus;
System.out.println(score);
Key Concept: Selection with if Statements
An if statement allows code to execute conditionally—only when a condition is true.
if (condition) {
// This code only runs if condition is true
}
// This code always runs (outside the if)
Key insight: The code inside curly braces {} only executes when the condition evaluates to true. If the condition is false, the code inside is skipped entirely.
Step-by-Step Trace
| Line | Code | score | bonus | Condition | Output |
|---|---|---|---|---|---|
| 1 | int score = 85; | 85 | - | - | |
| 2 | int bonus = 0; | 85 | 0 | - | |
| 3 | if (score >= 80) | 85 | 0 | 85 >= 80 → true | |
| 4 | bonus = 10; | 85 | 10 | (executes) | |
| 6 | score = score + bonus; | 95 | 10 | - | |
| 7 | println(score); | 95 | 10 | - | 95 |
Final output: 95
Why: Since 85 >= 80 is true, the if statement body executes, setting bonus to 10. Then score becomes 85 + 10 = 95.
Common Mistakes
This would be correct if we printed the threshold value from the condition, but we print score, not the number 80. The condition score >= 80 evaluates to true or false, it doesn't change score to 80.
This mistake comes from thinking the if statement doesn't execute, or forgetting that score gets updated on line 6. The condition 85 >= 80 is true, so bonus becomes 10, and then score = 85 + 10 = 95, not 85.
This is close but uses the wrong arithmetic. Some students might think bonus = 5 instead of 10, or miscalculate 85 + 10. Always trace carefully and double-check your arithmetic.
This prints the value of bonus instead of score. The println statement prints score, which is 95 after the addition, not bonus which is 10.
Understanding If Statement Flow
// Step 1: Evaluate the condition
if (score >= 80) // Is 85 >= 80? YES → true
// Step 2: Since true, execute the body
{
bonus = 10; // This line DOES execute
}
// Step 3: Continue with code after if
score = score + bonus; // Always executes (outside if)
If the condition were false:
int score = 75; // Changed to 75
int bonus = 0;
if (score >= 80) // Is 75 >= 80? NO → false
{
bonus = 10; // This line SKIPPED
}
score = score + bonus; // 75 + 0 = 75
System.out.println(score); // Prints 75
Practice Technique
When tracing if statements, ask yourself these questions in order:
- What is the condition? (Write it down)
- What are the current variable values? (Check your trace table)
- Substitute values into the condition (e.g., 85 >= 80)
- Evaluate: true or false? (Be precise)
- If true: Execute the body, update variables
- If false: Skip the body entirely
- Continue with code after the if
Never guess—always trace!
Additional Examples
int temp = 72;
String message = "Normal";
if (temp > 75) {
message = "Hot";
}
System.out.println(message); // Prints "Normal"
// Because 72 > 75 is false, if body skipped
int grade = 88;
if (grade >= 90) {
grade = grade + 5; // Skipped (88 >= 90 is false)
}
System.out.println(grade); // Prints 88
int x = 100;
int y = 50;
if (x > y) { // 100 > 50 is true
x = x - 20; // x becomes 80
y = y + 20; // y becomes 70
}
System.out.println(x + " " + y); // Prints "80 70"
Related Topics
- Section 2.2: Boolean Expressions (conditions that evaluate to true/false)
- Section 2.3: if Statements and Control Flow (single-path decisions)
- Section 2.4: if-else Statements (two-path decisions)
- Section 2.7: while Loops (repetition with conditions)
- Section 2.9: Implementing Algorithms with Selection
Ready to Level Up Your AP CSA Skills?
Get personalized help or access our complete question bank
Premium Question Bank - Coming Soon! Schedule 1-on-1 Tutoring400+ Unit 2 questions • Expert tutoring with 1,700+ hours experience • 5.0 rating