If Statement Worksheet

For each of the following, correct the syntax errors.

1.  if x > 25.0
       y = x
    else
       y = z;

2.  if (x >> 0);
       cout << "positive " << x << endl;
    else;
       cout << "negative " << x << endl;




What is the output of each of the following statements? Assume that x = 5, y = 2, z = 10, and temp = 0

3. if (y >= x) y = z; cout << x << " " << y << " " << z << endl; 4. if (y >= x) { y = z; cout << x << " " << y << " " << z << endl; } 5. if (z < y) temp = x; x = z; z = temp; cout << x << " " << y << " " << z << endl; 6. if (z > y) { temp = x; x = z; z = temp; } cout << x << " " << y << " " << z << endl; 7. if (x >= 6) cout << x + y << endl; cout << x + y << endl; 8. if (x + y > z) x = y + z; else x = y - z; cout << x << " " << y << " " << z << endl;

Write if statements for the following problems. Use meaningful variable names. You can assume that the variables you use are declared and have been initialized.

14. If a variable angle is equal to 90 degrees, print the message "right
    angle. Otherwise print the message "not a right angle".

15. If the difference between variables temp1 and temp2 is more than 2.3,
    set the variable approx to (temp1 - temp2) * factor.

16. A student at State U is ready to graduate if he or she has completed
    at least 122 credits. Write the statements to test whether a student
    is ready to graduate, and print a message telling whether he or she
    is ready.


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

© Copyright Emmi Schatz 2002