Developing Software Solutions
Conditional Statements
There
are many wasys of asking a question in a program. The reason for asking a
question could be to save doing unnecessary calculations or to check that the
user is no entering DUMB data.
Example 3.1
This uses an IF...THEN...ELSE
statement. If you enter a number between 4.5 and 5.5 the output is
"ACCEPTABLE" otherwise it is "UNACCEPTABLE"
PROGRAM
TestNumber;
VAR
Number:real;
BEGIN
Clrscr;
Write('Enter the number: N= ');
Read (Number);
Writeln;
Writeln;
IF Number >=4.5 THEN
IF Number <=5.5 THEN
Writeln('ACCEPTABLE')
ELSE
Writeln('UNACCEPTABLE')
ELSE
Writeln('UNACCEPTABLE')
END.
Example 3.2
This uses a CASE...ELSE which is
useful if there are more than two answers to a question. Note that there are
two END statements.
PROGRAM
Colour:
VAR
Entry:integer;
BEGIN
Clrscr;
Writeln('Enter a number 1,2,3 or 4
and a colour name is displayed');
Read(Entry);
CASE Entry OF
1: Writeln('Brown');
2: Writeln ('Red');
3: Writeln('Orange');
4:Writeln('Yellow');
ELSE
Writeln('This is an
invalid entry');
END
END.
Looping Operations
A
looping operation is a way to get the computer to do the same job over and over
again.
Example 3.3
This
is a WHILE/DO LOOP. note how TESTSCORE and COUNT are set to zero at the
beginning. The WRITE statement in the loop to tell you what the COUNT is each time.
PROGRAM
Test_Average;
VAR
Number, Count: integer;
TestScore, Sum, Average:real;
BEGIN
Clrscr;
Writeln('This program will work out
the average of any number');
Writeln('of test scores');
Writeln;
Writeln;
Write('Enter the number of scores you
wish to average: Number= ');
Readln(Number);
Writeln;
Writeln;
TestScore:=0;
Count:=0;
WHILE Count < Number DO
BEGIN
Count:=Count+1;
Write('Enter
test score #',Count,': ');
Readln(TestScore);
Sum:=Sum+TestScore
END; {while}
Average:=Sum/Number;
Writeln;
Writeln;
Writeln('The average of
',Number,'scores is ',Average:5:2)
END.
Exercise 3.4
Write
a Pascal program. It should be handwritten on A4 paper.
You
can use a WHILE/DO LOOP to work out reciprocal equation.
S:=S+1/R
This
refers to Tasks 1.3,1.4, 1.5
This is due 28th February
This is a REPEAT/UNTIL LOOP. Note
how COUNT is set to 0. This program
works out the sum of numbers entered.
PROGRAM
SUM;
VAR
Number, Count:integer;
RT,R1,R:real;
BEGIN
Clrscr;
Writeln('This program will work out
the sum of numbers entered');
Writeln;
Writeln;
Write('Enter the number of numbers
to be added: Number = ';
Readln(Number);
Writeln;
Count:=0;
REPEAT
Count:=Count+1;
Write('Enter number
#',Count,' = ');
Read(R);
IF Count=1 THEN
RT:=R
ELSE
RT:=RT+R
UNTIL Count = Number;
Write ('The total sum is RT);
END.
This is a FOR/DO LOOP. Note how
TestScore is set to zero.
PROGRAM
Test_Average;
VAR
Number, Count:integer;
TestScore,Sum,Average:real;
BEGIN
Clrscr;
Writeln('This program will work out
the average of test scores');
Writeln;
Writeln;
Write('Enter the number of scores
you wish to average: Number = ');
Readln(Number);
Writeln;
Writeln;
TestScore:=0;
FOR Count:=1 TO Number DO
BEGIN
Write('Enter
test score #',Count,': ';
Readln(TestScore);
Sum:=Sum+TestScore
END; {for loop}
Average:=Sum/Number;
Writeln;
Writeln;
Writeln('The average of ',Number,'
scores is ',Average:5:2)
END.