Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
Switch case statement in C++ language

Switch case statement in C++ language

Posted on February 13, 2017June 26, 2020

Table of Contents

  • Switch case statement in C++ language
    • Switch statements
      • How to work this statements

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

" using namespace std; int main() { int ch; cout<<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

" #include <conio.h> using namespace std; int main() { char ch; int num1,num2; cout <<

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
Similar post
Switch-case statements in C
break statement in C language
Suggetse for you
If statements in C language
If statements in C++ language
If statements in Java language

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes