C++ Programming

C++ Programming

The Switch Statement


Introduction

C++ provides a multiple-branch selection statement known as switch. It is denoted by the switch keyword. This selection statement successively tests the value of an expression against a list of integer or character constants. When the match found, the statements associated with that constant are executed.

The syntax of the switch statement is as follows: 

switch( expression )
{
case constant1: statement1;
                         break;
case constant2: statement2;
                         break;
case constant3: statement3;
                         break;
default: default_statement;
                         break;
}

Note: If the expression is a character constant, then the constant must be enclosed in single quotes.

The expression is evaluated and its values are matched against the values of the constants specified in the case statements. When a match is found, the statement associated with that case is executed until the break statement or the end of the switch statement is reached.

If a case statement does not include a break statement, then the control continues right on the next case statement until either a break is encountered or the end of the switch statement is reached. This situation ( missing break in case statement) is referred to as the fall through. The default statement is represented by the default keyword and gets executed when no match is found. The default statement is optional and if it is missing, no action takes place if all matches fail.

The ANSI standard specifies that a switch can have upto 257 case statements, however you must always limit the number of case statements to a smaller amount for the sake of efficiency.

A case cannot exist by itself, outside of a switch. The break statement, used under switch is one C++ jump statements. You’ll learn more about jump statement in a later section of this chapter. When a break statement is encountered in a switch statement, program execution jumps to the line of code following the statement that is outside the body of the switch statement.

While using switch, you can optimize your code by putting the most common cases first. This way unnecessary comparisons and thereby wastage of CPU time may be avoided. We’ll create a program based on switch statement in a later section of the module.

switch Vs if-else

The switch and if-else both are selection statements and they both let you select an alternative out of given many alternatives by testing an expression. However, there are some differences in their operations. These are given below:

The Nested-Switch

Like if statements, a switch statement can also be nested. There can be a switch as part of the statement sequence of another switch.

The syntax for a nested-switch is as follows:

switch(expression1)
{
case constant1: switch( expression2 )
                          {
                            case constantA: statement2;
                                                     break;
                            case constantB: statement3;
                                                     break;
                          }
                         break;
case constant2: statement4;
break;

}

Interesting Facts about Switch Statement

There are some important things that you must know about the switch statement :

Note: A switch statement is more efficient than the nested if-else statement

The Weekly Program

Now let’s create a program to input number week’s day and to translate its equivalent name of the day of the week.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int day;
cout<<"Enter number of week's day (1-7): ";
cin>>day;
switch(day)
{
case 1 : cout<<"Sunday";
             break;
case 2 : cout<<"\nMonday";
             break;
case 3 : cout<<"\nTuesday";
             break;
case 4 : cout<<"\nWednesday";
             break;
case 5 : cout<<"\nThursday";
             break;
case 6 : cout<<"\nFriday";
             break;
case 7 : cout<<"\nSaturday";
             break;
default : cout<<"\nInvalid Choice";
               break;
}
getch();
}

The day is been put in the switch parentheses. This means that whatever number the user inputs( 1-7 ), The corresponding case label will be executed.

Execution of the Program

Now Compile the program. If the compiler shows errors then check my program and resolve it. Then Run the program. Here’s the output:

Now it’s time to do it yourself and feel free to mess around with the program to understand in-depth what each statement does. Don’t forget to save your program.

That’s it for today, see you next week!!!