Is continue allowed directly inside a switch statement without an enclosing loop?\n\n#include<stdio.h>\nint main()\n{\n int i=3;\n switch(i)\n {\n case 1:\n printf("Hello\n");\n case 2:\n printf("Hi\n");\n case 3:\n continue;\n default:\n printf("Bye\n");\n }\n return 0;\n}

Difficulty: Easy

Correct Answer: Error: Misplaced continue

Explanation:


Introduction / Context:
This question checks knowledge of valid control statements inside a switch. While break and goto are common within a switch, continue is only meaningful within loops (for, while, do-while).



Given Data / Assumptions:

  • continue appears directly in a switch within main, not inside any loop.
  • Compiler follows standard C rules for control flow statements.


Concept / Approach:
continue transfers control to the next loop iteration. Without an enclosing loop, the statement has no valid target and is ill-formed. Most compilers emit an error such as “continue statement not within a loop” or “misplaced continue”.



Step-by-Step Solution:
Parse the switch: cases 1, 2, and 3 exist; default also exists.Encounter continue; in case 3 without an enclosing loop.Compilation fails with an error about continue’s placement.


Verification / Alternative check:
Wrap the switch inside a loop to make continue valid: e.g., while(1){ switch(i){ ... case 3: continue; }} Now continue would jump to the next loop iteration.



Why Other Options Are Wrong:
No runtime output occurs because compilation fails. Therefore options suggesting printed lines or a hang are incorrect.



Common Pitfalls:
Confusing break (valid in switch) with continue (loop-only); thinking continue behaves like break in a switch.



Final Answer:
Error: Misplaced continue

More Questions from Control Instructions

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion