switch Integer multiway selection statement

switch Integer multiway selection statement

switch selects a case by integer value, or default. Fall-through can share code or surprise; break or return exits. No floating-point or string controls.

CategoryControl flow
KindKeyword / Statement
C standardC89 / C90

📝 Syntax

switch (n) { case 1: ...; break; default: ...; }

▶ Example

Edit the C code below and click Run; the server compiles and executes it:

Output:

one
one or two

📚 Related Tutorials

❓ FAQ

QCan switch test strings or floating-point values directly?
ANo. Its control expression must have integer or enumeration type; string pointers and floating-point values are invalid. String dispatch normally uses an if chain with comparisons such as strcmp.
QMust every case in a switch include break?
ANo. break is only one way to leave a switch; return can leave the function, and consecutive labels or intentional fall-through can share code. Choose the control flow deliberately and document it clearly.
QCan an ordinary variable be used as the value after case?
ANo. A case label requires an integer constant expression. An ordinary variable does not qualify even if it is never changed after initialization. Use if statements or another lookup structure for runtime dispatch.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏