ProgrammingModerateTCS NQT · Accenture

How many times does the loop body run?

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

Correct answer: A. 2

The pairs (i, j) are (0, 0) and (1, 2), and then j reaches 4 so the test fails. The body runs twice.

More C Programming questions

Exams that ask this topic