switch statement

It is similar to the nested-if, but in nested-if where the code of line increases, in order to understand the program and read it becomes a bit difficult.

To solve this problem, we use the switch statement in the program. The switch statement is the execution step by step and every statement terminates with “break” keyword (except for the default statement), in which case the expression value is true, the statement becomes execute.

It has a statement default statement which executes when the expression value of all the statements is false.

SYNTEX:

switch (input)
 {
   case <1-exp>: first case body; break;

   case <2-exp>: second case body; break;

   case <3-exp>: third case body; break;

   default: body of default;
 };

Here is the flow-diagram of switch,

switch-statement

Here is the program

#include<iostream.h>
#include<conio.h>

 void main()
 {
  clrscr(); 
  int x;
  cout<<"Enter number: ";
  cin>>x;

    switch(x)
    {
      case 1: cout<<"Good Morning";break;
      case 4: cout<<"Good Night
      case 2: cout<<"Good Evening";break;
      case 3: cout<<"Good AfterNoon";break;

       default:cout<<" Try again..";
    }

  getch();
}
OUTPUT:-
Enter number: 2
Good Evening

Enter number: 5
Try again..

The break keyword is used in the switch statement to define a case statement body. If we do not use break keywords like-

{
  case 1:cout<<"Good Morning";
  case 2:cout <<"Good Night";
  break;
}

In such a situation, if the expression value is true in case 1, case 2 will also automatically execute even expression value is false in case 2. But expression value is true in case 2 then only case 2 statement will execute. In this way we can say that case 2 statement will execute in two ways. First, if the expression value of case 1 is true and second own statement is true.

If more than one expression value is true in SELECTION STATEMENT then all statements will be executed one by one.

Extra

creating menu

Here is another example of switch, where each case statement take input from the user,

#include<iostream.h>
#include<conio.h>

void main()
{
 clrscr();
 int choice;
 int num1,num2,result;

 cout<<"1.Sum of two Number\n";
 cout<<"2.Subtraction of two Number\n";
 cout<<"3.Multiply of two Number\n";

 cout<<"\nEnter Your Choice: ";
 cin>>choice;

     switch(choice)
      {
        case 1:
                  cout<<"\nEnter Two Number: ";
                  cin>>num1>>num2;
                  result = num1+num2;
                  cout<<"\nTotal: "<<result;
                  break;

        case 2:
                  cout<<"\nEnter Two Number: ";
                  cin>>num1>>num2;
                  result = num1-num2;
                  cout<<"\nSubstraction: "<<result;
                  break;

        case 3:
                  cout<<"\nEnter Two Number: ";
                  cin>>num1>>num2;
                  result = num1*num2;
                  cout<<"\nMultiply: "<<result;
                  break;

       default:
	      cout<<"\ninvalid choice....";
      }

  getch();
 }
OUTPUT:-
1.Sum of two Number
2.Subtraction of two Number
3.Multiply of two Number

Enter Your Choice:1

Enter Two Number:4 5 
Total: 9

In a good programming habit, the same code should not be written repeatedly.

As we can see in the above program, the same statement has been repeated repeatedly in every case-statement of the switch statement, which would not be considered a good programming habit but its good for programming practice.

This program is also created by the function in a better way. function with switch.

You have noticed, programs related to the switch behave like a menu. Therefore, be it C++, or any other language switch-statement is mostly used to create menus in program.


previous-Control Statement and their type

next-loop and their types