Saturday, 25 July 2015

Use of Switch Statement in C++



                          Use of Switch Statement in C++


Basic Purpose Switch Statement:

The switch-case statement is a multi-way decision statement. Unlike the multiple decision statement that can be created using if-else, the switch statement evaluates the conditional expression and tests it against numerous constant values. The branch corresponding to the value that the expression matches is taken during execution.

 Expression used in Switch Statement:

The value of the expressions in a switch-case statement must be an ordinal type i.e. integer, char, short, long, etc. Float and double are not allowed.

Syntax of switch:


 The syntax is : 
 switch( expression ) 
 { 
 case constant-expression1: statements1;
 [case constant-expression2: statements2;] 
[case constant-expression3: statements3;]
 [default : statements4;] 
 } 

The case statements and the default statement can occur in any order in the switch statement. The default clause is an optional clause that is matched if none of the constants in the case statements can be matched.
 Consider the example shown below:

 switch( Grade )
 { 
 case 'A' : printf( "Excellent" );
 case 'B' : printf( "Good" );
 case 'C' : printf( "OK" );
 case 'D' : printf( "Mmmmm...." );
 case 'F' : printf( "You must do better than this" );
 default : printf( "What is your grade anyway?" );
 }

 Here, if the Grade is 'A' then the output will be

 Excellent
 Good
 OK
 Mmmmm.... 
 You must do better than this What is your grade anyway?
 This is because, in the 'C' switch statement, execution continues on into the next case clause if it is not explicitly specified that the execution should exit the switch statement. The correct statement would be
:
 switch( Grade )
 { case 'A' : printf( "Excellent" );
 break; 
 case 'B' : printf( "Good" );
 break; 
 case 'C' : printf( "OK" );
 break;
 case 'D' : printf( "Mmmmm...." );
 break; 
 case 'F' : printf( "You must do better than this" );
 break;
 default : printf( "What is your grade anyway?" );
 break; 
 }

 Although the break in the default clause (or in general, after the last clause) is not necessary, it is good programming practice to put it in anyway

 Exercise:

 • Create the equivalent of a four function calculator. The program should request the user to enter a number which determines the mathematical operation to be carried out. It should then carry out the specified operation(addition, multiplication , subtraction , division ) use a switch statement to determine the operation and finally show the output. The program should ask the user if he/she wants to carry out an other operation at the end of every operation.
 • A mail order house sells five different products whose products retail prices are :

 Product 1---Rs 100
 Product 2—Rs 150 
Product 3—Rs 75 
Product 2—Rs 200 

Write a program that reads a series of pair of numbers as follows 
a)product number
 b)quantity sold 
your program should use a switch statement to determine the retail price for each product. Your program should calculate and display the total retail value of all products sold. 

No comments:

Post a Comment