The switch()… statement is ideally used when we have a single value that can be from a specified set of values only. Doesn't that sound like an enumerated type? It should and, happily, it does.
Using the switch statement to evaluate an enumerated type simplifies the intent of our code and helps to prevent some troublesome situations. Here is the shapeFunc() function revisited using theswitch()… statement:
enum_result_code shapeFunc( enum shape aShape)
{
...
switch( aShape )
{
case triangle:
...
break;
case rectangle:
...
break;
case circle:
...
break;
default:
... // Error: unhandled enumerated type. Clean up, alert user, return.
return unHandledEnumeration;
break;
}
...
return noError; // Normal end.
}
By using theswitch()…statement, it is clear we are considering only the value of shape. Recall that using if()…else...