Switch Statement In C++

Understand Switch Statement in C++ By Making A Simple Calculator

Posted by Zohaib Qazi on may 15, 2017

Switch Statement In C++

After reading this post you will get to know that What are switch statement in C++. what is a Case. And How to make a simple calculator in switch statement. However, clear your concept by reading this post and watching our Balochi video which is placed below.

Switch Statement

A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case. However, the flow diagram is shown below.

Switch Statement In C++ Video in Balochi

Switch Statement C++ code by balochprogrammer


#include < iostream >
using namespace std;
int main()
{
double num1, num2;

char opt;
double result;

cout<<"Enter the number One : ";
cin>>num1;
cout<<"Enter the number Two : ";
cin>>num2;
cout<<"Enter the Operator (* , + , /, - ): ";
cin>>opt;


switch(opt){
case '+':
result = num1 + num2;
cout<<"Addition :" << result << endl ;
break;
case '-':
result = num1 - num2;
cout<<"Subtraction :" << result << endl ;
break;
case '*':
result = num1 * num2;
cout<<"Multiplication :" << result << endl ;
break;
case '/':
result = num1 / num2;
cout<<"Division :" << result << endl ;
break;
default :
cout<<"Wrong Input";
return 0 ;
}