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