Wyo C++ - Ch. 7 Worksheet #2 Name -

Use good style when writing if and switch statements. Use curly braces around all if and else clauses to avoid making dangling else errors.

1. Use some kind of if statement (or if/else, if/elseif, nested if, etc.) that displays the value of the integer variable number if its value is greater than 0, less than 30, and prime. (Note that 1 and 2 are considered to be prime numbers. Hint: use the % operator.)






2. What is the value (true or false) of the following possible if statement control expressions where a, b, & c are integer variables with the following values: a = 1, b = 2 & c = 3.

a/ (a < b)

b/ (a < b || c == 2)

c/ (b + a)

d/ (2 * a + -b)

e/ (!(c > b) && a > b)

f/ (b * c % 2 == 0)

g/ (b * c % 2 )

h/ (c / 4)

3. Write an if/else statement that displays the message "within range " if the value of the integer variable number is greater than or equal to 5 but less than 10. Otherwise, the message "not finished" displays.

 




4. Write a switch structure that displays the message "failed" if the value of the integer variable quizScore is less than 6. The switch statement should display the message "average" if quizScore is 6, 7, or 8 and it should display "good" if quizScore is 9 or 10. Otherwise, the message "illegal value" should be displayed. You can continue your answer on the back of this paper.








5. List the following operators in order of overall priority from highest to lowest. For example, with regard to arithmetic operations * is considered to have a higher priority than +.

<
%

+
||
 == (double equals)
&&