Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

C++ example to demonstrate difference between switch and else if statements

#include <iostream>
using namespace std;
int main()
{
    char g = 'B';
    cout << "use switch\n";
    switch (g) {
        case 'A':
            cout << "\t80+\n";
            break;
        case 'B':
            cout << "\t70 .. 79\n";
            break;
        case 'c': // no break, continue to next statement
        case 'C':
            cout << "\t60 .. 69\n";
            break;
        default:
            cout << "very sorry\n";
    }
    cout << "use if" << endl;
    
    if (g == 'A')
        cout << "\t80+\n";
    else if (g == 'B')
        cout << "\t70 .. 79\n";
    else if (g == 'C' or g == 'c')
        cout << "\t60 .. 69\n"; // or is the same as ||
    else
        cout << "very sorry\n";
        
    return 0;
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.