HOW TO WRITE A PROGRAM

CONDITIONAL STATEMENT

SYNTAX:

if  BooleanExpression  then

    --statements here are executed if expression is true

elseif  BooleanExpression  then

--statements here are executed if Boolean condition is true

else

  --statements here executed if all conditions are false

end --if


WORD PROBLEM

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 sales price.

When writing a program, always define sample input and output first.  Later in development, the documentation will be used for testing.

INPUT OUTPUT
12000 $240.00
24000 $720.00
25000 $750.00
40000 $2000.00

HOW TO PROCEED?

How?  refer to Formulas and Tables
print("Sale\tCommission")
print("$3000\t$"..(0.02*3000.0))
print("$3500\t$"..(0.02*3500.0))
print("$4000\t$"..(0.02*4000.0))

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
print("Sale\tPercent\tCommission")
commision = carPrice * percent
print("$"..carPrice.."\t"..(percent*100).."%\t$"..commision

carPrice = 11000
11000.0
Sale        Percent     Commission
$11000    2%            $220


USE if TO ADD CONDITIONAL TESTS
Replace the percent= assignment statement.

What are the conditions?

CONDITION

percent fraction

sale<=12000.0 0.02
sale<=25000.0 0.03
sale>25000.0 0.05

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

else

 percent = 0.05    --statements executed if all are false

end --if

TRY OUT ALL TESTS LISTED EARLIER
VERIFY OUTPUT!!
DONE!!!