LearnFrenzy
Sign in Take a free test

C Loops · Predict the Output · Question 10

while (i++ < 3) – final values (output) Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 10 Back to Exercise

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int i = 0, n = 0;
    while (i++ < 3)
        n++;
    printf("%d %d\n", n, i);
    return 0;
}
  • A 3 4 Correct
  • B 3 3
  • C 4 4
  • D 2 3
Correct Answer: Option A

The comparison uses the old value of i and then increments it. Tests 0, 1, 2 pass (n = 3); the test 3 < 3 fails but still increments i to 4.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links