Conditional algorithms have selection statements in them. Their control structure is known as Selection Control Structure.
The first illustration is that of the if-then construct.
The second illustration is that of the if-then-else construct.
Examples:
1. If grade is 70 or more, output Pass.
2. If grade is 70 or more, output Pass; otherwise output Fail.
Example Problem:
Write an algorithm that determines and outputs if the average of a student allows him to pass or fail; the passing mark is 70.
Solution #1: if-then construct
Print "Enter the average of a student"
input avg
if avg >= 70 then
print "Pass"
end if
input key
Solution #2: if-then-else construct
Print "Enter the average of a student"
input avg
if avg >= 70 then
print "Pass"
else
print "Fail"
end if
input key
Now you try one:
Write an algorithm that prompts the user to enter the name and cost of two items. The algorithm should calculate and output the total cost. If the total exceeds $25.00 then offer a discount of 10%; otherwise a discount of 5%; show the new total after discount.
No comments:
Post a Comment