ProgrammingModerateTCS NQT · Infosys

How many iterations does this loop run?

How many iterations does this loop run?
#include <stdio.h>
int main(void) {
    int c = 0;
    for (int i = 10; i >= 0; i -= 3)
        c++;
    printf("%d", c);
}
  1. A. 3
  2. B. 4
  3. C. 5
  4. D. 0

Correct answer: B. 4

The loop visits i = 10, 7, 4 and 1, and then i becomes -2, which fails the test. So it runs four times.

More C Programming questions

Exams that ask this topic