LearnFrenzy
Sign in Take a free test

C Loops · Predict the Output · Question 4

break in the inner of two nested loops – output Answer, explanation & discussion

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

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    for (int i = 1; i <= 3; i++) {
        for (int j = 1; j <= 3; j++) {
            if (j == 2)
                break;
            printf("%d%d ", i, j);
        }
    }
    printf("\n");
    return 0;
}
  • A 11 21 31 Correct
  • B 11
  • C 11 12 13
  • D 11 21 31 12
Correct Answer: Option A

For each i, the inner loop prints j = 1 and breaks at j = 2. break leaves only the inner loop, so the outer loop continues with the next i.

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