LearnFrenzy
Sign in Take a free test

Control Instructions · Predict the Output · Question 11

break inside a nested switch – output Answer, explanation & discussion

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

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int x = 1, y = 2;
    switch (x) {
    case 1:
        switch (y) {
        case 2:  printf("x1y2 "); break;
        default: printf("x1 ");
        }
        printf("after-inner ");
        break;
    case 2:
        printf("x2 ");
    }
    printf("\n");
    return 0;
}
  • A x1y2 after-inner Correct
  • B x1y2
  • C x1y2 x1 after-inner
  • D x1y2 after-inner x2
Correct Answer: Option A

The inner break leaves only the inner switch, so "after-inner" still prints; the outer break then prevents falling into case 2.

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