You are on page 1of 4

Software Design & Development - Preliminary Course Page - 1

Programming Example No 2 Selection IF THEN

Problem;
Input a mark from a test and grade it into Pass, 50 - 100 or Fail, less than 50.

Step 1.
Define the problem.

Inputs Processing Outputs


Mark Grade mark into Pass
Pass 50 - 100 Fail
Fail < 50

Step 2.
Outline the solution.

a) Processing Steps;
? Marks between 50 - 100 = Pass
? Marks less than 50 = Fail

b) Subtasks;
None

c) Control Structure;
Selection
IF-THEN-ELSE

d) Variables;
Marks

e) Mainline Logic;
Take the mark input by the user. Determine the range that it falls into and then show the result.

© Tardiani Publishing 2000


Software Design & Development - Preliminary Course Page - 2

Step 3.
Solution Algorithm
BEGIN
BEGIN
READ mark READ
IF mark >= 50 THEN mark
DISPLAY “Pass
ELSE
DISPLAY “Fail mark DISPLAY
Yes
END IF >= 50 Pass
END ?
No
DISPLAY
Fail

END

Step 4
Deskcheck the Algorithm
First choose the test data.
Lets choose marks that will give obvious results, 75 and 25, plus some at the limits 50, 49, and 50.5

Input data First Second Third Fourth Fifth

mark 75 25 50 49 50.5

Variable First Second Third Fourth Fifth

mark Pass Fail Pass Fail Pass

Now establish the expected results of the test data

© 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 IF statement DISPLAY


executed
First Pass Read 75
IF yes
ELSE no
output Pass

Second Pass Read 25


IF no
ELSE yes
output Fail

Third Pass Read 50


IF yes
ELSE no
output Pass

Fourth Pass Read 49


IF no
ELSE yes
output Fail

Fifth Pass Read 50.5


IF yes
ELSE no
output Pass

© Tardiani Publishing 2000


Software Design & Development - Preliminary Course Page - 4

Step 5.
Programming Code

LOGO
To Grade :mark
IFELSE :mark > 50
[PRINT "Pass]
[PRINT "Fail]
END
Note: There is a error in the above program. Find it and correct it.

QUICK BASIC
'Grade
CLS
INPUT "Mark"; mark
IF mark >= 50 THEN
PRINT "Pass
ELSE
PRINT "Fail
END IF
END

PASCAL
program Grade(input, output);
var
mark : integer;
begin
writeln('Enter a mark');
readln(mark);
if (mark >= 50) then begin
write('Pass');
end
else begin
write('Fail');
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