Professional Documents
Culture Documents
08 If Then Else Selection
08 If Then Else Selection
Problem;
Design a program that will grade the marks input into:
Fail < 55
Pass 55 to < 85
Distinction 85 - 100.
Step 1.
Define the problem.
Step 2.
Outline the solution.
a) Processing Steps;
? Grade the marks above 54.9 as a Pass
? Grade the marks above 84.9 as a Distinction
? Give all other marks a Fail
b) Subtasks;
None
c) Control Structure;
Selection
IF THEN ELSE
d) Variables;
Grade inputs by user
e) Mainline Logic;
Have the user insert a mark. Process the mark so that the program determines whether it should be
awarded a Fail, Pass or a Distinction. Finally display the grade awarded.
Step 3.
BEGIN
Solution Algorithm
READ mark
BEGIN Grade
READ mark
IF mark >= 85 THEN Y
DISPLAY "Distinction >= 85
Distinction
ELSE
IF mark >= 55 THEN N
DISPLAY "Pass
ELSE Y
>= 55 Pass
DISPLAY "Fail
ENDIF
N
ENDIF
END
Y
< 55 Fail
END
Step 4
Deskcheck the Algorithm
First choose the test data. First test the obvious values 90, 60 and 25, plus the boundaries 85 and 55.
Now establish the expected results of the test data
mark 90 60 25 85 55
Now setup a table of the relevant variable names and pass each test data set through the solution
algorithm, statement by statement.
Step 5.
Programming Code
LOGO
TO Grade :mark
IFELSE :mark > 84.5
[PRINT "Distinction]
[IFELSE :mark > 54.9
[PRINT "Pass]
[PRINT "Fail]]
END
QUICK BASIC
INPUT "Mark"; mark
IF mark >= 85 THEN
PRINT "Distinction"
ELSE
IF mark >= 55 THEN
PRINT "Pass"
ELSE
PRINT "Fail"
END IF
END IF
PASCAL
program Grade(input, output);
{Grade a mark into Distinction, Pass and Fail}
var
mark : integer;
begin
writeln('Enter a mark.');
readln(mark);
if mark >= 85 then begin
writeln('Distinction');
end
else begin
if mark >= 55 then begin
writeln('Pass');
end
else begin
writeln('Fail');
end;
end;
end. Step 6.
Test the program
Use as many inputs as possible. Especially test
those inputs which are close to the boundaries of
the decisions to be made by the program.