if BooleanExpression then
--statements here are executed if expression is true
elseif BooleanExpression then
--statements here are executed if Boolean condition is true
--statements here executed if all conditions are false
A car dealer pays salespersons a
2% commission on all cars sold for $15,000 or less, 3% on all
cars sold for $25,000 or less, and 5% on all cars over
$25,000. Write program that prints out the amount for
the sale's commission check after reading the sales
price. Since Input is covered in a later section, just
assume that a carPrice variable has been initialized to the
When writing a program, always
define sample input and output first. Later in
development, the documentation will be used for testing.
HOW TO PROCEED?
What is the formula? x% * carPrice == 0.0x * carPrice
What data type is needed? real
What variable names? carPrice, percent, commision
One solution would be to print a table of commissions in increments of $500, then give a copy to every salesperson.
How? refer to
Formulas and Tables
Disadvantages? A table is not what was asked for!
CONDITION TO CHECK: Sales Amount
Must be checked when the program is run
conditional 'if' statement meets this requirement
ALWAYS TRY TO PROGRAM
WITHOUT CONDITIONS FIRST!! A program without 'if' statements is
easier to get right.
carPrice = 0.00 --change as needed
print(carPrice) --just for debugging
percent = 0.02 --fix the commission
commision = carPrice * percent
carPrice = 11000
Sale Percent Commission
$11000 2% $220
USE if TO ADD CONDITIONAL TESTS
Replace the percent= assignment statement.
What are the conditions?
if sale<=12000.0 then
percent = 0.02 --statements executed if BoolExp is true
elseif sale<=25000.0 then --copy elseif for multiple conditions
percent = 0.03 --statements executed if condition is true
percent = 0.05 --statements executed if all are false
TRY OUT ALL TESTS LISTED EARLIER