Using Switch Statement in C++

Consider a situation in which, only one block of code needs to be executed among many blocks. This type of situation can be handled using nested if...else statement but, the better way of handling this type of problem is using switch...case statement.

Consider a situation in which, only one block of code needs to be executed among many blocks. This type of situation can be handled using nested if...else statement but, the better way of handling this type of problem is using switch...case statement.

Using Switch Statement in C++

ORIGINAL SOURCE CODE = PROGRAMIZ

#include <iostream>
using namespace std;
int main() {
    char o;
    float num1,num2;
    cout<<"Select an operator either + or - or * or / \n";
    cin>>o;
    cout<<"Enter two operands: ";
    cin>>num1>>num2;
    switch(o) {
        case '+':
            cout<<num1<<" + "<<num2<<" = "<<num1+num2;
            break;
        case '-':
            cout<<num1<<" - "<<num2<<" = "<<num1-num2;
            break;
        case '*':
            cout<<num1<<" * "<<num2<<" = "<<num1*num2;
            break;
        case '/':
            cout<<num1<<" / "<<num2<<" = "<<num1/num2;
            break;
        default:
            /* If operator is other than +, -, * or /, error message is shown */
            printf("Error! operator is not correct");
            break;
    }
    return 0;
}

COMMENTS

Name

Blogger-Templates,1,Blogging,4,C++,11,Facebook,1,Free-Premium,1,Freebies,4,Google,1,Hack-Facebook,1,Programming,11,SEO,3,Social-Media,1,Softwares,3,Widgets,1,
ltr
item
Online Blogger Tricks | A2Z Art of Blogging: Using Switch Statement in C++
Using Switch Statement in C++
Consider a situation in which, only one block of code needs to be executed among many blocks. This type of situation can be handled using nested if...else statement but, the better way of handling this type of problem is using switch...case statement.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgLXXKD3w4IxGXhnVHzY0U-MXzuzXeesbUD_AIh1j2Lp3N5GscXKLB6_vqHwlfaGoGsSQB59z4W9c79bN2Rmga2h4VVwCdZkcaKq7CKE1qOdHea3brV3tI7etfLUG4CfuoUefT9XL3EsRk/s1600/switch+statement.png
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgLXXKD3w4IxGXhnVHzY0U-MXzuzXeesbUD_AIh1j2Lp3N5GscXKLB6_vqHwlfaGoGsSQB59z4W9c79bN2Rmga2h4VVwCdZkcaKq7CKE1qOdHea3brV3tI7etfLUG4CfuoUefT9XL3EsRk/s72-c/switch+statement.png
Online Blogger Tricks | A2Z Art of Blogging
https://a4abidaziz.blogspot.com/2016/03/using-switch-statement-in-c.html
https://a4abidaziz.blogspot.com/
http://a4abidaziz.blogspot.com/
http://a4abidaziz.blogspot.com/2016/03/using-switch-statement-in-c.html
true
1578912806520322323
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy