Multiple-Choice Strategy
Practice AP CS A multiple choice with code tracing, elimination, edge cases, time management, and topic recognition.
A lesson of AP Computer Science A, in AP CS A Exam Readiness, about 10 minutes.
Control Flow: Conditionals and Loops
By the end of this lesson, you will be able to write
if,else if, andelsestatements, usewhileandforloops, and trace through code to determine its output.
Boolean Expressions
Relational operators:
| Operator | Meaning |
|---|---|
== | equal to |
!= | not equal to |
< | less than |
> | greater than |
<= | less than or equal to |
>= | greater than or equal to |
Logical operators:
| Operator | Meaning | Example |
|---|---|---|
&& | AND (both must be true) | x > 0 && x < 10 |
|| | OR (at least one must be true) | x < 0 || x > 100 |
! | NOT (negation) | !done |
Short-circuit evaluation:
&&: if the left side isfalse, the right side is NOT evaluated||: if the left side istrue, the right side is NOT evaluated
if / else if / else
if (condition1) {
// executes if condition1 is true
} else if (condition2) {
// executes if condition1 is false AND condition2 is true
} else {
// executes if all conditions above are false
}
Example:
int score = 78;
String grade;
if (score >= 90) {
grade = "A";
} else if (score >= 80) {
grade = "B";
} else if (score >= 70) {
grade = "C";
} else if (score >= 60) {
grade = "D";
} else {
grade = "F";
}
// grade is "C"
Key point: once a condition is true and its block executes, the remaining else if and else blocks are skipped.