ProgrammingHardTCS NQT · Wipro

How many inner iterations run in total? Compute with this program.

How many inner iterations run in total? Compute with this program.
#include <stdio.h>
int main(void) {
    int i = 0, c = 0;
    do {
        for (int j = 0; j < 3; j++) {
            if (j == 1) break;
            c++;
        }
        i++;
    } while (i < 2);
    printf("%d", c);
}
  1. A. 4
  2. B. 1
  3. C. 3
  4. D. 2

Correct answer: D. 2

Each pass through the outer loop increments c once for j = 0 and then breaks at j = 1, so the two passes give 2.

More C Programming questions

Exams that ask this topic