LearnFrenzy
Sign in Take a free test

C Loops · Predict the Output · Question 11

break and continue inside a switch in a loop Answer, explanation & discussion

  • Output
  • Hard
  • 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) {
    for (int i = 0; i < 4; i++) {
        switch (i) {
        case 1:
            printf("one ");
            break;
        case 2:
            printf("two ");
            continue;
        default:
            printf("%d ", i);
        }
        printf("| ");
    }
    printf("\n");
    return 0;
}
  • A 0 | one | two 3 | Correct
  • B 0 | one | two | 3 |
  • C 0 | one
  • D 0 one two 3
Correct Answer: Option A

Inside a switch, break leaves only the switch, so "| " still prints after "one". continue is not used by switch: it goes to the next loop pass, skipping the "| " after "two".

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