Q:

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

belongs to collection: C++ find output programs

0

This section contains the C++ find output programs with their explanations on switch statements (set 1).

Program 1:

#include <iostream>
using namespace std;

int main()
{
    switch (printf("Hello World")) {
    case 0x09:
        cout << " India";
        break;

    case 0x0A:
        cout << " Australia";
        break;

    case 0x0B:
        cout << " USA";
        break;

    case 0x0C:
        cout << " England";
        break;
    }
    return 0;
}

Program 2:

#include <iostream>
using namespace std;

int main()
{
    int A = 10, B = 5, C = 2;

    switch (A * ++B + C - 8) {
    default:
        cout << "Pakistan ";
    case 0x09:
        cout << "India ";
        break;
    case 0x0A:
        cout << "Australia ";
        break;
    case 0x0B:
        cout << "USA ";
        break;
    case 0x0C:
        cout << "England ";
        break;
    }

    return 0;
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Program 1:-

Output:

Hello World USA

Explanation:

The above code will print "Hello World USA" on the console screen.

As we know that printf() function prints data on the console screen and returns the total number of characters printed on the screen.

printf("Hello World")

The above printf() function returns 11. And the hexadecimal equivalent of 11 is 0x0B, then case 0x0B will be executed.

And the final output is "Hello World USA".

Answer Program 2:-

Output:

Pakistan India

Explanation:

The above code will print "Pakistan India " on the console screen. Here we pass an expression in the switch block. Based on the expression result, the case will be executed. 

Evaluate the expression step by step:

int A=10,B=5,C=2
=A*++B+C-8
=10*6+2-8
=60+2-8
=54

The result of the expression is 54 and there is no such case is defined. Thus, the default case will be executed and then the case 0x09 will be executed (Because break statement is missing after in default case)

Then the final output "Pakistan India" will be printed on the console screen.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ find output programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ Switch Statements | Find output programs | Set... >>
<< C++ Conditional Statements | Find output programs ...