Introduction to C Programming

Introduction to C Programming by Rob Miles, Electronic Engineering


Switching

Making Multiple Decisions

Making Multiple Decisions

We now know nearly everything you need to know about constructing a program in the C language. You may find it rather surprising, but there is really very little left to know about programming itself. Most of the rest of C is concerned with making the business of programming simpler. A good example of this is the switch construction. Suppose you are refining your double glazing program to allow your customer to select from a pre-defined range of windows. You ask something like

Enter the type of window:

1 = casement
2 = standard
3 = patio door

Your program can then calculate the cost of the appropriate window by selecting type and giving the size.Each function asks the relevant questions and works out the price of that kind of item.

When you come to write the program you will probably end up with

something like:

void handle_casement (void)
	....
	.... definition of handle_casement
	....
void handle_standard (void)
	....
	.... definition of handle_standard
	....
void handle_patio (void)
	....
	.... definition of handle_patio
	....
void main (void)
{
	char select ;
	printf ("\n 1 = casement") ; 
	printf ("\n 2 = standard") ; 
	printf ("\n 3 = patio door") ;
	printf ("\nEnter the type of window : ") ; 
	scanf ("%c", &selection) ;	
	if (selection == '1') { 
		handle_casement () ;
	}
	if (selection == '2') { 
		handle_standard () ;
	}
	if (selection == '3') {
		handle_patio () ;
	}
}

This would work OK, but is rather clumsy. You have to write a large number of if constructions to activate each option.

Because you have to do this a lot C contains a special construction to allow you to select one option from a number of them based on a particular value. This is called the switch construction. If you write the above using it your program would look like this.

void main (void)
	char selection ;
	printf ("\nEnter the type of window : ") ;
	scanf ("%c", &selection) ;
	switch (selection){
	case '1' : handle_casement ();
		break ;
	case '2' : handle_standard () ;
		break ;
	case '3' : handle_patio () ;
		break ;
}

The switch command take a value which it uses to decide which option to perform. It executes the case which matches the value of the switch variable. Of course this means that the type of the cases that you use must match the switch selection value although, in true C tradition, the compiler will not tell you if you get it wrong, it will just delight in doing the wrong thing! One other possible naughty is the use of a float type for the switch selection. This is not allowed: because floating point numbers cannot be held exactly it would sometimes be impossible to find a match.

This mistake is high in the top ten programming errors, if you find more than one option being called make sure you have all your breaks!
The break statement after the call of the relevant routine is to stop the program running on and performing the code which follows. In the same way as you break out of a loop, when the break is reached the switch is finished and the program continues running at the statement after the switch. Note that unless you give a break C will continue to run down past all further cases, i.e. the case item just says where to start running in the switch, not where to stop. Consider the following :
int i ;
i = 1 ;
switch ( i ) {
	case 1 :
		printf ( "one\n" ) ;
	case 2 : 
		printf ( "two"\n ) ;
	break ;
}

Because there is no break at the end of the handling of the case for 1, the program would print out :

one
two

You can use this to good effect if you want a certain case to be selected by more than one value of the switch variable :

case 'c' :
case 'C' :
case '1' : 
	handle_casement () ;
	break ;

This would cause handle_casement to be called if the user pressed 'C', 'c' or '1'. You can have as many additional options as you like.

Another other useful feature is the default option. This gives the switch somewhere to go if the switch value doesn't match any of the cases available; in our case (sorry!) we put out an appropriate message, for example :

void main ()
	char selection ;
	printf ("\nEnter the type of window : ") ;
	scanf ("%c", &selection) ;
	switch (selection){
	case '1' : handle_casement ();
		break ;
	case '2' : handle_standard () ;
		break ;
	case '3' : handle_patio () ;
		break ;
	default : printf ("\nInvalid command\n")
}

The program would print out "Invalid Command" if anything other than a value with a matching case was entered.


Rob Miles, R.S.Miles@e-eng.hull.ac.uk, Electronic Engineering
HTML by Bronwen Reid, July 1995