C++

Switch case statement in C++ language

Switch case statement in C++ language

In this tutorial, we will discuss the switch case statement in C++ language.

The C++ switch-case statement executes one statement using multiple conditions. However, this statement checks a variable for equality against list of values.

Switch statements

Essential points about this statements

  • The switch statement is one of the java statement allows a unique variable to be checked for equality against a list of the case value of the same type variable(int, String, char can use anyone )
  • Switch case similar if-else statement functioning based Boolean expression.
  • This statement choose and executes one from multiple conditions, that multiple conditions are called  cases
  • Any number of the case may use within a switch and every case statement contain a value to compared with switch value. case and case value separated by a colon(case: value)
  • End of the switch  can have a default statement the default statement can be used for performing a task when becoming false

Syntax

switch(expression){
      case constant- expression 1:
                      ststements; // code to be executed if expression is equal to constant 1
                      break;
case constant- expression 2:
                      ststements;// code to be executed if expression is equal to constant 2
                      break;
//any number of case statement
default;
                      statements; //code to be executed if n doesn't  match any constant
}

The switch statement checks the expression. If it is found to match the case statement, control of the program passes to the block of codes associated with the case statements. The break statement is used to stop the control and prevent from running to the next case.

Flow diagram

How to work this statements

  • The switch statement is evaluated once
  • The value of the switch expression is compared with the value of each case.
  • When there is a match of the particular case, the associated block of statements is executed.
  • When there is not match any cases, the default statement is executed.

 

Program 1

Program 2

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    char ch;
    int num1,num2;
    cout << "Enter an operator as you wish!" << endl;
    cin>>ch;

    cout << "Enter two numbers for your calculation!" << endl;
    cin>>num1>>num2;

    switch(ch)
    {
    case '+':
        cout<<"Addition of "<<num1<<" and "<<num2<<": "<<num1+num2;
        break;

        case '-':
        cout<<"difference of "<<num1<<" and "<<num2<<": "<<num1-num2;
        break;

        case '*':
        cout<<"product of "<<num1<<" and "<<num2<<": "<<num1*num2;
        break;

        case '/':
        cout<<"division of "<<num1<<" and "<<num2<<": "<<num1/num2;
        break;
    }
    getch();
    return 0;
}

 

The above code is executed it produces the following result

Case 1

Enter an operator as you wish!

+

Enter two numbers for your calculation!

12

23

Addition of 12 and 23: 35

Case 2

Enter an operator as you wish!

Enter two numbers for your calculation!

45

23

the difference of 45 and 23: 22

Case 3

Enter an operator as you wish!

*

Enter two numbers for your calculation!

12

13

the product of 12 and 13: 156

Case 4

Enter an operator as you wish!

/

Enter two numbers for your calculation!

200

20

division of 200 and 20 : 10

In the above program,

The operator is entered by the user as they wish and it is stored in the ch variable.

Then two numbers are entered by the user and stored in variables num1 and num2 respectively

Next, the control of the program moves to the relevant case and displays the output

Finally, the break statement ends the switch statements

 

Program 3

 

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int score;
    cout << "Enter your score (1 to 100):" << endl;
    cin>>score;

     switch(score/10){
 case 10:
 case 9:
 case 8:

    cout<<"you got merit pass";
    break;

    case 7:
 case 6:

    cout<<"you have passed with first division";
    break;

      case 5:

    cout<<"you have passed with second division";
    break;

    case 4:
        case 3:

    cout<<"you have passed with third division";
    break;

     default:
     cout<<"Sorry, you try again once";
     }
     getch();
    return 0;
}

The above code is executed it produces the following result

Enter your score ( 1 to 100):
56
you have passed with second division
Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.