Introduction - Getting Started
Flow of Control
The Switch Statement
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;
case constant2: statement2;
case constant3: statement3;
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:
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:
case constant1: switch( expression2 )
case constantA: statement2;
case constantB: statement3;
case constant2: statement4;
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.
cout<<"Enter number of week's day (1-7): ";
case 1 : cout<<"Sunday";
case 2 : cout<<"\nMonday";
case 3 : cout<<"\nTuesday";
case 4 : cout<<"\nWednesday";
case 5 : cout<<"\nThursday";
case 6 : cout<<"\nFriday";
case 7 : cout<<"\nSaturday";
default : cout<<"\nInvalid Choice";
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!!!