November 2001
Beginner to intermediate
816 pages
16h 3m
English
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 ... |
Read now
Unlock full access