LearnFrenzy
Sign in Take a free test

Control Instructions · Predict the Output · Question 9

A loop built with goto – output Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 9 Back to Exercise

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int i = 1;
again:
    printf("%d ", i);
    i *= 2;
    if (i < 20)
        goto again;
    printf("\n");
    return 0;
}
  • A 1 2 4 8 16 Correct
  • B 1 2 4 8 16 32
  • C 2 4 8 16
  • D 1 2 4 8
Correct Answer: Option A

The backward goto behaves like a do-while: print, double, repeat while i < 20. After 16, i becomes 32 and the jump is not taken. A real loop would express this more clearly.

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