while Loops

AP Computer Science A· difficulty 4/5

"Read numbers until -1 is entered, then stop" — best loop pattern

  • A

    do { input = read(); } while (input != -1);

  • B

    for (int i = 0; i < n; i++)

  • C

    Both A and C are reasonable

    check_circle
  • D

    while (input != -1) { ...; input = read(); }

Explanation

Either pattern (priming-read while or do-while) handles sentinel.

Want 10 more like this — adaptive to your weak spots?

Related questions