5. If statements¶
Q) Fill in the IF's condition¶
Imagine you are creating the safety system for a bus. The bus can hold up to 20 people safely. Consider the following code.
cout << "How many people are on the bus?" << endl;
int numPeople = 0;
cin >> numPeople;
if (XXXXXXX) {
cout << "The bus is safe." << endl;
} else {
cout << "The buss is overloaded." << endl;
}
Which of the following options should replace the XXXXXXX?
a) numPeople <= 20
b) numPeople < 20
c) numPeople >= 20
d) numPeople > 20
Q) Nested-If¶
Consider the following code fragment:
double rainfall = ??????;
double temperature = ?????;
string message;
if (rainfall < 10) {
message = "Dry and ";
if (temperature >= 40) {
message += "hot";
} else {
message += "cold";
}
} else if (rainfall < 20) {
message = "Wet and ";
if (temperature >= 40) {
message += "warmer than usual";
} else {
message += "average";
}
} else {
message = "Far too wet";
}
cout << message << endl;
Which of the following values for rainfall
and temperature
make the above code print "Wet and average
"?
a) rainfall = 7.3, temperature = 53.5
b) rainfall = 5.6, temperature = 34.2
c) rainfall = 14.1, temperature = 53.2
d) rainfall = 16.2, temperature = 24.3
Answers¶
a) numPeople <= 20
d) rainfall = 16.2, temperature = 24.3