Starting Out with C++ Chapter 4
Rewrite the following if/else statements as conditional expressions if ( x > y ) z = 1; else z = 20;
z = x > y ? 1 : 20
Write an if/else statement that assigns 0.10 to commissionRate unless sales is greater than or equal to 50000.00, in which case it assigns 0.20 to comissionRate.
if (sales >= 50000.00) commissionRate = 0.20; else commissionRate = 0.10;
TRUE or FALSE: Both of the following if statement perform the same operations. if (calls == 20) rate *=0.5; if (calls = 20) rate *= 0.5;
FALSE, the first statement checks if calls are equal o 20 and the second statement assigns a value of 20 to the variable calls.
TRUE or FALSE: The following if/else statements cause the same output to display. if (x > y) cout << "x is the greater. \n" else cout << "x is not the greater. \n" if (y <=x) cout << "x is not the greater. \n" else cout << "x is the greater. \n"
FALSE. The first if statement only checks that x is greater than y in order to output x is the greater. The second if statement checks that y is less than or equal to x or just alike if x is greater than or equal to y to output a different message stating x is not the greater.
Explain why you cannot convert the following if else if statement into a switch statement. if (temp == 100) x = 0; else if (population > 100 ) x = 1; else if (rate < .1 ) x = -1;
The reason you cannot convert the following if else if statement into a switch statement is because the if else if statement tests the conditions of different variables and in order to use a switch statement we must be testing the conditions of the same variable.
What is wrong with the following switch statement? switch (temp) { case temp < 0 : cout << "Temp is negative. \n"; break; case temp == 0 : cout << "Temp is zero. \n"; break; case temp > 0 : cout << "Temp is positive. \n"; break; }
The variable must be followed by an integer CONSTANT not a relational operator.
Write an if statement that performs the following logic: if the variable sales is greater than 50,000, then assign 0.25 to the comissionRate variable and assign 250 to the bonus variable.
if (sales > 50000) { comissionRate = 0.25; bonus = 250; }
Write an if statement that performs the following logic: if the variable price is greater than 500, then assign 0.2 to the variable discountRate.
if (price > 500) discountRate = 0.2;
Write an if statement that prints the message "The number is not valid" if the variable speed is outside the range 0 through 200
if (speed <= 0 && speed >= 200) cout << "The number is not valid.";
Write an if statement that prints the message "The number is valid" if the variable speed is within the range 0 through 200
if (speed >= 0 && speed <= 200) cout << "The number is valid.";
Write an if statement that performs the following logic: if the variable x is equal to 20, then assign 0 to the variable y
if (x == 20) y = 0;
Write an if/ else statement that assigns 1 to x if y is equal to 100. Otherwise it should assign 0 to x.
if (y ==100) x = 1; else x = 0;