switch Statements
When there are many conditions to evaluate, the if-else if-else statement can become complex and verbose. A much cleaner solution for some situations is the switch statement. The switch statement allows testing any integral value or string against multiple values. When the test produces a match, all statements associated with that match are executed. Here's the basic form of a switch statement:
switch(integral or string expression) { case <literal-1>: statement(s) break; . . . case <literal-n>: statement(s) break; [default: statement(s)] }
For C++ ProgrammersC++ accepts only integer values in a switch statement. C# accepts strings and enums as well as integers. Also, C++ permits case fall-through. C# does not. Break statements ... |
Get C# Unleashed now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.