ProgrammingModerateTCS NQT · Accenture

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    int i = 5;
    do {
        i++;
    } while (i < 3);
    printf("%d", i);
}
  1. A. 5
  2. B. 6
  3. C. Nothing: the loop never ends
  4. D. 7

Correct answer: B. 6

A do-while runs its body before the first test, so i becomes 6; then 6 < 3 is false and the loop exits. 5 treats it as a while loop, 7 runs the body twice, and the infinite-loop answer confuses do-while with repeat-until.

More C Programming questions

Exams that ask this topic