ProgrammingHardTCS NQT · Infosys

What is the output?

What is the output?
int i = 0, s = 0;
do {
    i++;
    if (i % 2) continue;
    s += i;
} while (i < 7);
printf("%d %d", i, s);
  1. A. Nothing: it loops forever
  2. B. 6 12
  3. C. 8 20
  4. D. 7 12

Correct answer: D. 7 12

In a do-while, continue jumps to the condition test, not back to the top, so the loop still ends when i reaches 7. Only the even values 2, 4, 6 are added: s = 12. The infinite-loop option assumes continue skips the test.

More C Programming questions

Exams that ask this topic