for Loops

AP Computer Science A· difficulty 2/5

int i = 0;
while (i < 5) {
    System.out.println(i);
    i++;
}

Which for-loop is equivalent to the loop above?

  • A

    for (int i = 0; i <= 5; i++) System.out.println(i);

  • B

    for (int i = 1; i < 5; i++) System.out.println(i);

  • C

    for (int i = 0; i < 5; i--) System.out.println(i);

  • D

    for (int i = 0; i < 5; i++) System.out.println(i);

    check_circle

Explanation

Both loops start at 0, run while i < 5, and increment by one after each iteration, printing 0,1,2,3,4.

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

Related questions