Switch Statement in C++

The switch statement is a special multiway decision maker that tests whether an expression matches one of the number of constant values accordingly. switch statement allows the user to choose a statement or a group of statements among several alternatives. The switch statement is useful when a variable is compared with different constants, and in case it is equal to a constant, a set of statements would be executed.

The general form of the switch statement is as follows:

switch (expression) {
case constant 1:
statement;
case constant 2:
statement;
..........
..........
case constant n:
statement;
default:
statement;
}

In this general form:
1) The expression, whose value is being compared, may be any valid expression but not a floating point expression
2) The The value that follows the keyword case may only be constants. They cannot be expressions. They may be an integer or characters, but not the floating point numbers or character string
3) The constants in each of the case statements must obviously be of same type
4) The expression’s value is checked against each of the specified case and when the match occurs, the statement following the case is executed. Again, to maintain generality, the statement can be either a simple or a compound statement
5) The last case of this statement which is called the default case is optional and should be used according to the program’s specific requirement. If no match is found, then the statement in the default case would be executed. If the default case is not included in a switch statement and the expression is not matched by any other cases, nothing happens and the statement following the switch construct is executed.

Execution of the switch in C++ follows a different logic. No statements are executed until a case has been matched or the default case has been encountered. However, it continues to execute all statements once a case has been matched; irrespective of the fact that whether those statements belong to the case that has been matched or not.

C++ offers a method of overcoming this side-effect of switch statement with the help of the break statement. The break statement will cause an immediate exit from the switch construct.

In general, it is advisable to use the break statement.

Thus the general form of the mutually exclusive switch statement will be:

switch (expression) {
case constant 1:
statement;
break;
case constant 2:
statement;
break;
..........
..........
case constant n:
statement;
break;
default:
statement;
}

Example:
Program which reads a number, between 1 to 7 and then prints the day corresponding to that number

#include<iostream.h>
void main()
{
int n;
cout << "Enter a day number between 1 to 7 :";
cin >> n;
switch(n){
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
case 4:
cout << "Thursday";
break;
case 5:
cout << "Friday";
break;
case 6:
cout << "Saturday";
break;
case 7:
cout << "Sunday";
break;
default:
cout << "Enter correct number";
}
}

You may also like...

2 Responses

  1. September 22, 2013

    […] Switch Statement […]

  2. October 5, 2013

    […] switch Statement […]

Leave a Reply

Your email address will not be published. Required fields are marked *