More If Statements Worksheet

What is the output of each of the following statements? Assume that a = 10, b = 5, c = 8, and temp = 0

1.  if (a <= b)
       b = a;
    cout << a << "  " << b << endl;

2.  if (a <= b)
    {
       b = a;
       cout << a << "  " << b << endl;
    }

3.  if (a < b)
       temp = a;
    a = b;
    b = temp;
    cout << a << "  " << b << endl;

4.  if (a < b)
    {
       temp = a;
       a = b;
       b = temp;
    }
    cout << a << "  " << b << endl;

5.  if (a < b)
       if (b + c < a)
          cout << c << "  " << b << endl;
       else
          cout << b << "  " << c << endl;
    cout << b << "  " << a << endl;

6.  if (a > b)
       if (b + c < a)
          cout << c << "  " << a << endl;
       else
          cout << b << "  " << c << endl;
    else
       cout << b << "  " << a << endl;

7.  if (a > b)
    {
       c = c + b;
       if (c > a)
          cout << temp << "  " << c << endl;
       else
       {
          b = 2 * b;
          cout << b << "  " << c << endl;
       }
    }
    else
    {
       a += 5;
       cout << a << "  " << b << endl;
    }

8.  if (a < c && b > temp)
       a += c;
    else
       a -= c;
    cout << a << "  " << b << "  " << c << endl;

9.  if (a < c || b > temp)
       b = a + c;
    else
       c = a + b;
    cout << a << "  " << b << "  " << c << endl;

10. if (a - b > c && temp != 0)
       temp++;
    else
       temp--;
    cout << temp << endl;

Write boolean expressions for each of the following conditions:

11. x is greater than 3

12. y is between 2 and 5, but not equal to 2 or 5

13. r is negative and z is positive

14. a and b are both positive

15. a is less than 6 or is greater than 10

16. x is less than 3 and is greater than or equal to zero

Write the C++ statements for each of the following:

17. Read a person's age and print a message stating whether or not the
person is old enough to drink. The minimum age to drink is 21.

18. Read a person's age and print a message stating whether the person
is too young to drive, old enough to drive but too young to drink, or old
enough to drive and drink (but not at the same time!). The minimum age to
drive is 17.

19. Read an area code. If the area code is 732, 973, or 908, print the
message "Local call". Otherwise print the message "Long distance".

20. Read an area code. If the area code is 732 or 848, print the
message "Local call". If the area code is 201, 551, 609, 856, 862,
908, or 973, print the message "In NJ". Otherwise, print the message
"Out of state".

21. Read the temperature and print one of the following messages:
             Temperature          Message
             more than 83         hot
             68 to 83             ideal
             45 to 67             cool
             less than 45         cold

22. To calculate bonuses, a company uses the profit factor, along with
an employee's salary, job type and job level. For a manager (job type
'm') at level 1 through 3, the bonus is calculated as 3% of the salary
multiplied by the profit factor. Otherwise, a manager receives a bonus
of 4% of salary multiplied by the profit factor. For a salesperson
(job type 's') at any level, the bonus is calculated as 5% of salary
multiplied by the profit factor. For a technical employee (job type 't')
of level 1 through 5, the bonus is calculated as 5.5% of salary
multiplied by the profit factor. For a technical employee of level 6 or
7, the bonus is calculated as 6% of salary multiplied by the profit
factor. For a technical employee of level 8, the bonus is calculated as
6.5% of salary multiplied by the profit factor. All administrative
employees (job type 'a') receive a bonus of 5% of salary multiplied by
the profit factor.

Write the statements to read the profit factor, job type, job level, and
salary, and then calculate and print the bonus.

Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2002