transfer if-else statement into switch statement

my if else is as well as I espective, BUT SWITCH STATEMENT is the one I need help on....

How to make if else code below into SWITCH STATEMENT?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 cout << "Enter the temperature: ";
    cin >> temp;
    cout << "Enter the pressure: ";
    cin >> pressure;
    cout <<endl;

    if(temp >= 200 && pressure >= 500)
        cout << "Warning! the temperature and pressure are too HIGH."<<endl;

    else if(temp >= 200)
        cout << "Warning! the temperature is too HIGH."<<endl;

    else if(pressure >= 500)
        cout << "Warning! the pressure is too HIGH."<<endl;

    else
        cout << "The temperature and pressure are OK."<<endl;



I need help in this code below... make the above code (if else) in SWITCH STATEMENT.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
        cout << "Enter the temperature: ";
        cin >> temp;

   switch(temp)
   {
        case 1 ... 199:
            cout<<"*********************"<<endl;
            cout << "The temperature is OK"<<endl;
            cout<<"*********************"<<endl;
            break;

        default:
            cout<<"*********************************"<<endl;
            cout << "Warning! the temperature too HIGH"<<endl;
            cout<<"*********************************"<<endl;
    }

        cout<<endl;
        cout << "Enter the pressure: ";
        cin >> pressure;

    switch(pressure)
    {
        case 1 ... 499:
            cout<<"******************"<<endl;
            cout << "The pressure is OK"<<endl;
            cout<<"******************"<<endl;
            break;

        default:
            cout<<"******************************"<<endl;
            cout << "Warning! the pressure too HIGH"<<endl;
            cout<<"******************************"<<endl;
    }
Switch statements can only check against constants so you would need to write:
1
2
3
4
5
6
7
8
9
10
11
12
13
switch(temp)
{
    case 1: 
    case 2: 
    case 3: 
    ... 
    case 199: 
         cout << "Temp ok\n"
         break;
    default: 
        cout << "Temp too high\n"; 
        break;
}


Which is not good use of a switch (unless you love typing?). If/else is far the better choice here.
Last edited on
lol ... I know that, but my instructor want us to write on both statements and I kind a have problem with the switch statements.

Thanks for suggest and reply... appreciate that. (^_^)
What problems do you have with switch statements? If you do as you did above.
1
2
3
4
5
6
7
8
9
10
11
12
switch(variable)
{
    case 1:
        //Do something
        break;
    case 2: 
        //Do something 
        break;
     default: 
        //Do something 
        break;
}


This is fine.

Write an if-else statement that outputs the word Warning provided that either the value of the variable temperature is greater than or equal to 200 or the value of the variable pressure is greater than or equal to 500, or both. Otherwise the if-else statement outputs OK. The variables temperature and pressure are both of type int.


PROBLEM: Convert the above specification of a branching statement to a switch statement.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
using namespace std;

int main()
{
    int temperature, pressure;

        cout << "Enter the temperature: ";
        cin >> temperature;
        cout<<endl;
        cout << "Enter the pressure: ";
        cin >> pressure;
        cout<<endl;

        cout << "Below is IF-ELSE STATEMENT:"<<endl;
        cout << "***************************\n"<<endl;
        //if else statement is good to go...

        if(temperature >= 200 && pressure >= 500)
            cout<< "Warning!"<<endl;

        else if(temperature >= 200)
            cout << "Warning!"<<endl;

        else if(pressure >= 500)
            cout << "Warning!"<<endl;

        else
            cout << "OK!"<<endl;

        cout << "***************************\n"<<endl;
        cout << "Below is SWITCH STATEMENT:"<<endl;
        //problem is that the switch statement is not print out as if else statement

   switch(temperature)
   {
        case 1 ... 199:
            cout << "***"<<endl;
            cout << "OK!"<<endl;
            cout << "***"<<endl;
            break;

        default:
            cout<< "Warning!"<<endl;
            cout<< "********"<<endl;
            break;

        switch(pressure)
        {

            case 1 ... 499:
                cout << "OK!"<<endl;
                cout << "***"<<endl;
                break;

            default:
                cout<< "Warning!"<<endl;
                cout<< "********"<<endl;
                break;
        }
}

    return 0;
}



PROBLEM with SWITCH STATEMENT:

temp >= 200 --> good to go...
press >= 500 --> good to go...

print out: Warning!


broblem:

temp >= 199--> good to go...
press >= 500 --> good to go...

print out: OK instead Warning! as the instruction on if else.

niether (temp >= 200 or press >= 499) = warning since temp is >= 200
or (temp >= 199, but press >= 500) = warning since press >= 500
OR both: (temp >= 215 and press >= 554) = warning..
if both (temp >= 199 AND press >= 246) = OK

Last edited on
Do the switch statements one after the other. The first switch will reach a break; before the second switch is executed at the moment. i.e. Lines 48-60 will never run.
It's also possible to do it using only one switch statement. Switch on an expression involving both
(temperature >= 200) and (pressure >= 500). Remember that boolean values can be implicitly cast
to integer ones (false --> 0, true --> 1). BTW, this code -> case 1 ... 199: is not standard C++.
Last edited on
Topic archived. No new replies allowed.