Practice Exercise: Control Structures(selection)
Objective:
Be able to employ the if, if...else, if...else...if
The general form for the if statement is:
if (expression)
statement ...;
else ...;
Compound statement blocks are surrounded by {}.
1. Simple if statement: Given that an variable item named workHours has been
initialized or input, determine whether it is greater than 40, and print
the message "Pay Overtime!".
2. Two-Way Selection: Given a variable item named workHours, calculate
regularHours and overtimeHours. RegularHours are those workHours less
than or equal to 40. OvertimeHours are those workHours exceeding 40.
3. Complex if statements: Given data items for workStatus, payPlan,
annualSalary, and payYTD (pay for the year to this date).
For workStatus of 'A' (for active) and payPlan is 'M' (for monthly),
compute currentPay = annualSalary / 12; then add currentPay to payYTD.
4. Multiple Selection (nested if): Compute tax on currentPay according to
the following rules: For currentPay <= 2000.00 tax is 0.00; for currentPay
from 2000.00 to 2500.00, tax is 15% of currentPay over 2000.00
(first 2000.00 not taxed); and for currentPay over 2500.00, tax is $75 plus
20% of CurrentPay over 2500.00.