The switch statement


1.  The switch statement

When the logical_expression gives more integer results and for some of them program needs to perform different sections of the program then instead of if statement we need to use switch statement. When program comes to the switch statement block it first calculates the expression which gives a integer result. Depending on this integer result, the flow of the program is switched to some of the branches inside of the switch statement block. Syntax of the switch statement block is given below.


Switch ( integer_expression) {
Case constant_expression1 :
// first command block
Case constant_ expression 2 :
// second command block
break;
Case constant_expression 3 :
Case constant_expression 4 :
// third command block
break;
default:
// fourth command block
}
Now let’s explain how the switch statement block works. First of all the program calculates the integer_expression and the result of calculation of integer_expression has to be and integer number. If the result of this expression is equal to one of the cases given below, than the program executes the command block which follows corresponding case condition and the execution of the switch statement is going on until it reaches the break command. When the execution of the switch block arrives on a break command then the execution of the code in switch statement ends and continues from the first command after the switch block. If the result of the integer expression isn’t equal to any case in the switch statement block then the program executes the default command.
Now we will take a closer look into the previous syntax of the switch statement block. If the result of the switch statement gives an:
·         Constant_ expression1 , then the program will execute first command block, and then the second command block. When the program arrives at the command break the program skips execution of other commands and executes the first command after the switch statement block.
·         Constant_expression2, then the program executes the second command block. After program arrives on break command the program skips execution of other commands and executes the first command after the switch statement block.
·         Constant_expression3 or constatnt_expression4, then the program executes the third command block. When the program arrives at the command break the program skips execution of other command and executes the first command after the switch statement block.
·         If the integer result of the switch statement isn’t equal to none of the constant_expression, then the program executes the forth command block after the command break and after that program executes the first command below the switch statement block.
After detail explanation of the switch statement block we will apply the switch statement on a concrete example.
// FOOD.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using std::endl;
using std::cout;
using std::cin;
int _tmain(int argc, _TCHAR* argv[])
{
            int choice;
            cout << "MENU\n\n";
            cout << "1." << "\t" << "Lobster\n";
            cout << "2." << "\t" << "Steak\n";
            cout << "3." << "\t" << "Turkey\n";
            cout << "4." << "\t" << "Hamberger\n";
            cout << "5." << "\t" << "Vegetarian\n\n";
            cout << "Choose your dinner entree: ";
            cin >> choice;
            cout << endl;
            switch (choice)
            {
            case 1: cout << "Lobster is my favorite! Dig in!\n\n";
                        break;
            case 2: cout << "Yummy! Steak is great...\n"
                             << "but limit yourself to once a week," << endl
                             << "or risk the chance of high cholesterol!\n\n";
                        break;
            case 3: cout << "Turkey is healthier than steak! ...Enjoy!\n\n";
                        break;
            case 4: cout << "Hamburger is another form of steak. :-)\n\n";
                        break;
            case 5: cout <<  "Finally, a vegitarian is in the house!\n\n";
                        break;
            default: cout << "Invalid number, please enter a number"
                              << " from the entrees above.\n\n";
                        break;
            }
            return 0;
}
Share on Google Plus