You are writing a web application that will determine how much someone will rece
ID: 3819018 • Letter: Y
Question
You are writing a web application that will determine how much someone will receive as a discount when they purchase a certain dollar amount. If the user enters $200, based on the following code, what would the discount be?
#Declare Variables
Declare sale as float
Declare discount as float
#User enters in total sale and then assigned to dblSale variable
write "Enter in the Total Sale Amount: "
input sale
If sale > 50 Then
discount = .35 * sale
End If
If sale > 40 Then
discount = .25 * sale
End If
If sale > 30 Then
discount = .20 * sale
Else
discount = .10 * sale
End If
Explanation / Answer
Answer is 40
Reason:
sale amount is 200
discount variable is declared outside all if and else blocks so it;s global.
1st if condition that is if sale>50 will stand true and its block will be executed.
so that discount = 0.35*200 = 70
since there is if statement again instead of "else ... if" or "else" its condition will also be checked
if sale > 40 that also will become true.
so that
discount = 0.25*200 = 50
so now value of discount variable is overwritten by 2nd if block and that is 50 now.
again there is if block
if sale > 30 that will be true
so that
discount = 0.20* 200 = 40
so now value of discount variable is again overwritten by 3rd if block and that is 40 now.
so finally discount=40
P.S. It seems there is a mistake in writting the code .
else cannot be inside the if block itself. it must be outside the if block
so first end the last if block and then write "else"
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.