You are on page 1of 4

Software Design & Development - Preliminary Course Page - 1

Programming Example No 3 Selection IF THEN ELSE

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.

Inputs Processing Outputs


Marks Grade the marks Fail
Mark >= 85 = Distinction Pass
Mark >= 55 and < 85 = Pass Distinction
Mark < 55 = Fail

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.

© Tardiani Publishing 2000


Software Design & Development - Preliminary Course Page - 2

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

Input data First Second Third Fourth Fifth

mark 90 60 25 85 55

Variable First data Second Third Fourth Fifth

mark Distinction Pass Fail Distinction Pass

© Tardiani Publishing 2000


Software Design & Development - Preliminary Course Page - 3

Now setup a table of the relevant variable names and pass each test data set through the solution
algorithm, statement by statement.

Statement mark First IF Second IF Third IF DISPLAY

First Pass Read 90


IF true
ELSE..IF skipped
ELSE..IF skipped
DISPLAY Distinction

Second Pass Read 60


IF false
ELSE..IF true
ELSE..IF skipped
DISPLAY Pass

Third Pass Read 25


IF false
ELSE..IF false
ELSE..IF true
DISPLAY Fail

Fourth Pass Read 85


IF true
ELSE..IF skipped
ELSE..IF skipped
DISPLAY Distinction

Fifth Pass Read 55


IF false
ELSE..IF true
ELSE..IF skipped
DISPLAY Pass

© Tardiani Publishing 2000


Software Design & Development - Preliminary Course Page - 4

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.

© Tardiani Publishing 2000

You might also like