Case Control Instruction

Case-Control Instruction allows the computer to make a decision as to which instruction is to be executed next.

Switch Statement

The control statement that allows us to make a decision from the number of choices is called a switch, or more correctly a switch-case-default since these three keywords go together to make up the control statement. The switch statement is a sort of specialized form of if used to efficiently separate different blocks of code based on the value of an integer. The switch expression is evaluated, and then the flow of control jumps to the matching const-expression case. The case expressions are typically int or char constants.

They most often appear as follows:

switch ( expression ) 
{ 
case constant 1 : 
do this ; 
case constant 2 : 
do this ; 
case constant 3 : 
do this ; 
.
.
.
default : 
do this ; 
}

The integer expression following the keyword switch is any C expression that will yield an integer value. It could be an integer constant like 1, 2, or 3, or an expression that evaluates to an integer. The keyword case is followed by an integer or a character constant. Each constant in each case must be different from all the others. The “do this” lines in the above form of switch represent any valid C statement.

When we run a program containing a switch then First, the integer expression following the keyword switch is evaluated. The value it gives is then matched, one by one, against the constant values that follow the case statements. When a match is found, the program executes the statements following that case, and all subsequent case and default statements as well. If no match is found with any of the case statements, only the statements following the default are executed.

For example:-

main( ) 
{ 
int i = 2 ;
switch ( i ) 
{ 
case 1 : 
printf ( "I am in case 1 \n" ) ; 
break ; 
case 2 : 
printf ( "I am in case 2 \n" ) ; 
break ; 
case 3 : 
printf ( "I am in case 3 \n" ) ; 
break ; 
default : 
printf ( "I am in default \n" ) ; 
} 
}

The output of this program would be:

I am in case 2

In the above program, control will directly be transferred to case 2 because the value of the expression is evaluated to 2.

goto statement

It’s also a control structure that transfers the control of a program from one part to another part. In a difficult programming situation, it seems so easy to use a goto to take control where you want. However, almost always, there is a more elegant way of writing the same program using if, for, while, and switch. These constructs are far more logical and easy to understand.

The big problem with gotos is that when we do use them we can never be sure how we got to a certain point in our code. They obscure the flow of control. So as far as possible skip them. You can always get the job done without them. Trust me, with good programming skills goto can always be avoided. the following program shows how to use goto.

main( ) 
{ 
int goals ; 
printf ( "Enter the number of goals scored against India" ) ; 
scanf ( "%d", &goals ) ; 
if ( goals <= 5 ) 
goto label1 ; 
else 
{ 
printf ( "About time soccer players learnt C\n" ) ; 
printf ( "and said goodbye! adieu! to soccer" ) ; 
exit( ) ; /* terminates program execution */ 
} 
label1 : 
printf ( "To err is human!" ) ;
} 

And here are two sample runs of the program…

Enter the number of goals scored against India 3 
To err is human! 
Enter the number of goals scored against India 7 
About time soccer players learnt C 
and said goodbye! adieu! to soccer 

A few remarks about the program would make things clearer.

  • If the condition is satisfied the goto statement transfers control to the label ‘label1’, causing printf( ) following sos to be executed.
  • The label can be on a separate line or on the same line as the statement following it, as in, label1: printf ( “To err is human!” ) ;
  • Any number of gotos can take control of the same label.
  • The exit( ) function is a standard library function that terminates the execution of the program. It is necessary to use this function since we don’t want the statement.