switch Statement

There is one special case of the "if", "else if" sequence which occurs relatively frequently, where the conditional expressions are all comparing some value against a set of different constants (c1,c2,...):

	if (e==c1) s1;
	else if (e==c2) s2;
	else if (e==c3) s3;
	else if (e==c4) s4;
	else s5;

For this special case of a series of constant comparisons, the "switch" statement has been invented.

express the above using the "switch" statement in 'C', we would write the following:

	switch(e) {
		case c1: s1; break;
		case c2: s2; break;
		case c3: s3; break;
		case c4: s4; break;
	default: s5; break;
	} /* closing curly brace */

Note the required curly braces and how the first "if" and each "else if" is replaced by the uniform 'case' phrase. Also note that the last "else", the one without an "if", is replaced by "default". If this catch all condition is not wanted, then the "default" case would be left out. The "default" case can be place any where in the switch statement, even first.

If a "switch" statement does not have a "default" condition, then if the expression being evaluated does not match any of the "case" values nothing will be executed (other than the evaluation of the expression).

It is important to note that after each statement (s1,s2,s3,...) there is a "break" statement. In the context of a "switch" statement (i.e. as the enclosing statement for a "break" statement), the meaning of the "break" is to jump to the first statement after the closing curly brace of the "switch" statement. This avoids labels and goto's. Note also that even though there is more than one statement for each "case", curly braces are not required.

The expression 'e' must be of type "int" or smaller (e.g. char or short int).

The flow chart for the above examples is:

      |
      v
      ^          ^          ^          ^
     /  \       /  \       /  \       /  \
    <e,c1>--->-<e,c2>--->-<e,c3>--->-<e,c4>--->---\
     \  / !=    \  / !=    \  / !=    \  / !=     |
      v          v          v          v          |
      | ==       | ==       | ==       | ==       |
      v          v          v          v          v default
   +----+      +----+     +----+     +----+     +----+
   | s1 |      | s2 |     | s3 |     | s4 |     | s5 |
   +----+      +----+     +----+     +----+     +----+
      |          |          |          |          |
      v          v          v          v          v
      +-------------------------------------------/
      |
      v

Note the very regular structure, where '==' means 'equals, and '!=' means 'not equals'.

If one of the "break" statements is not present in one of the "case" arms or branches of the "switch" statement, then instead of the control going to the outside of the "switch" statement, control would continue to fall right through to the next "case" and the statement or statements it has. Thus if we had:

	switch(e) {
		case c1: s1; break;
		case c2: s2;
		case c3: s3; break;
		default: s4; break;
	} /* closing curly brace */

This would be equivalent to:

	if (e==c1) s1;
	else if (e==c2) { s2; goto label3; }
	else if (e==c3) label3: s3;
	else s4;

The flow chart for the above example is:

      |
      v
      ^          ^          ^
     /  \       /  \       /  \
    <e,c1>--->-<e,c2>--->-<e,c3>--->---\
     \  / !=    \  / !=    \  / !=     |
      v          v          v          v
      | ==       | ==  /--->| ==       |
      v          v     |    v          v
   +----+      +----+  |  +----+     +----+
   | s1 |      | s2 |  |  | s3 |     | s4 |
   +----+      +----+  |  +----+     +----+
      |          |     |    |          |
      |          \-----/    |          |
      v                     v          v
      +--------------------------------/
      |
      v

© 1991-2008 Prem Sobel. All Rights Reserved.