C++ Switch Statements | Find output programs | Set 2

This section contains the C++ find output programs with their explanations on switch statements (set 2).
Submitted by Nidhi, on June 02, 2020

Program 1:

#include <iostream>
using namespace std;

int main()
{
    int num = 0;

    num = 'A' - 2;

    switch (num) {
    case 0x30:
        cout << "India ";
        break;
    case 0x36:
        cout << "Australia ";
        break;
    case 0x39:
        cout << "USA ";
        break;
    case 0x41:
        cout << "England ";
        break;
    default:
        cout << "Pakistan ";
        break;
    }

    return 0;
}

Output:

Pakistan 

Explanation:

The program will print "Pakistan " on the console screen. Here we use an expression that is given below:

num = 'A' -2;

As we know that the ASCII value of the 'A' is 65. Here, we are subtracting 2 by 65 then the result will be 63. And the hexadecimal equivalent of 63 is 0x3F. Here we did not use any case for 0x3F. That's why the default case will execute.

Then the final output "Pakistan ".

Program 2:

#include <iostream>
#define ABC 2 + 3
using namespace std;

int main()
{
    int num = 0;

    num = ABC * ABC;
    switch (num) {
    case 10:
        cout << "India ";
        break;
    case 11:
        cout << "Australia ";
        break;
    case 25:
        cout << "USA ";
        break;
    default:
        cout << "England ";
        break;
    }

    return 0;
}

Output:

Australia 

Explanation:

The above code will print "Australia " on the console screen.

Understand the flow of execution step by step.

num = ABC*ABC;

In the above expression, we used macro ABC, it will expend then the expression will be:

num = 2+3*2+3
    = 2+6+3
    = 11

Then case 11 will execute. And prints "Australia " on the console screen.

Program 3:

#include <iostream>
using namespace std;

int main()
{
    char str[] = "12345";

    switch (*(str + 1)) {
    case '1':
        switch (*(str + 0)) {
        case '1':
            cout << "India";
            break;
        case '2':
            cout << "US";
            break;
        default:
            cout << "UK";
            break;
        }
        break;

    case '2':
        switch (*(str + 0)) {
        case '1':
            cout << "MP";
            break;
        case '2':
            cout << "UP";
            break;
        default:
            cout << "AP";
            break;
        }
        break;

    case '3':
        switch (*(str + 0)) {
        case '1':
            cout << "New York";
            break;
        case '2':
            cout << "New Jersy";
            break;
        default:
            cout << "Washington DC";
            break;
        }
        break;

    default:
        cout << "Scottland";
        break;
    }

    return 0;
}

Output:

MP

Explanation:

The above code will print "MP" on the console screen.

Understand the program step by step.

switch(*(str+1))

In the above switch statement, we are accessing a character of index 1 that is '2' from character array. Then case '2' will execute.

In the above program, we used nested switch blocks. Within the case '2' again we are accessing a character of index 0 from character array that is '1' then case '1' of the nested switch will be executed and it will print "MP" on the console screen.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.