Open in StudyFoxFree, no ads. Practice, track progress and quiz yourself in the app.
AP Computer Science A · Lesson 3 of 6 · 9 min read
Boolean logic:&& (and), || (or), ! (not). Java uses short-circuit evaluation: in a && b, if a is false, b isn't checked.
De Morgan's laws:!(a && b) equals !a || !b; !(a || b) equals !a && !b.
Loops:while runs while a condition is true; for is best for counting. Watch for off-by-one errors.
Common algorithms: sum, average, count matches, find min or max, and check if any or all elements meet a condition.
// Count the vowels in a word
int count = 0;
for (int i = 0; i < word.length(); i++) {
if ("aeiou".indexOf(word.substring(i, i + 1)) >= 0) {
count++;
}
}
Check your understanding
How many times does this loop run? for (int i = 2; i < 10; i += 3)
2
3
4
8
Show answer
Answer: B. i takes the values 2, 5, and 8. At 11 the condition fails, so it runs 3 times.