StudyFox

Selection and iteration

Open in StudyFox Free, no ads. Practice, track progress and quiz yourself in the app.

AP Computer Science A · Lesson 3 of 6 · 9 min read

// 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)

  1. 2
  2. 3
  3. 4
  4. 8
Show answer

Answer: B. i takes the values 2, 5, and 8. At 11 the condition fails, so it runs 3 times.