POH CHAN KAI MEMORIAL COLLEGE

F. 4 Computer Studies

Class: ______( ) Name: ______Date:______

Practical 5: Conditional and Branching Statements

1. Jim writes a program to study the reserved words 'IFTHENELSE':

PROGRAM P501;

VAR password : STRING;

BEGIN

WRITE('ENTER YOUR PASSWORD ... ');

READLN(password);

IF (password = ’ABC’) THEN

WRITELN('HI JIM, HOW DO YOU DO?')

ELSE

WRITELN('INCORRECT PASSWORD!')

END.

Run the program in your computer, try the following input and write down the output.

INPUT / OUTPUT
12345
ABC
A B C

2. The following program is to find the largest of any 3 given integers.

PROGRAM P502;

VAR a, b, c, max : INTEGER;

BEGIN

WRITE('INPUT A, B, C');

READLN(a, b, c);

max := a;

IF (b > max) THEN max := b;

IF (c > max) THEN max := c;

WRITELN('THE LARGEST ONE IS ',max)

END.

(a) The function of the line statement READLN(a, b, c) is to

______

(b) The function of the first IF statement is

______

(c) Run the program in your computer, try the following input and write down the computer output.

INPUT / OUTPUT
3 9 2
-7 0 6

(d) If the two IF statements are combined with an ELSE statement as follows:

IF ( b > max ) THEN max := b

ELSE

I F ( c > max ) THEN max := c;

Test the modified program with input of 1 2 3 .

Does the program perform the same function? (YES/NO)______

3. Study the following Pascal program.

PROGRAM P503;

VAR opt : 0..10;

BEGIN

WRITE('INPUT YOUR OPTION ... ');

READLN(opt);

CASE opt OF

1 : WRITELN('THIS IS OPTION ONE.');

2 : WRITELN('THIS IS OPTION TWO.');

3 : WRITELN('THIS IS OPTION THREE.')

END

END.

(a) Run the above program in your computer, write down the output if the input of opt is

INPUT / OUTPUT
1
2
3
4

(b) Rewrite the statements in between CASEEND with the IFTHEN-ELSE reserved words only.

______

______

______

______

______

______

______

______

______

______

______

(c) Change the CASE block to the following by adding an ELSE line:

CASE opt OF

1 : WRITELN('THIS IS OPTION ONE.');

2 : WRITELN('THIS IS OPTION TWO.');

3 : WRITELN('THIS IS OPTION THREE.');

ELSE WRITELN('INVALID OPTION!')

END

Run the above program in your computer, write down the output if the input of opt is

INPUT / OUTPUT
1
3
4
7

4. ABC company sets the following rules for defining the unit price of a battery product during the promotion period.

Number Of Batteries Ordered / Unit Price
1 50 / $25.00
51 100 / $24.50
101 200 / $24.00
More than 200 / $23.00

Write a program on the program sheet provided to do ALL of the followings:

(a) Declare variables of suitable types:

Variable nameMeaning

num_batthe number of batteries to be ordered

unit_pricethe corresponding unit price

amountthe total amount

______

______

______

______

(b) Write the program segment to input the number of batteries to be ordered.

Program to calculate the amount charged with discount.

Please enter the number of batteries ordered: 120

______

______

(c) By using IF-THEN-ELSE conditional structures, write a program segment to determine the unit price of such an order, then output the unit price.

The unit price is $24.00 .

______

______

______

______

______

______

______

______

______

______

______

(d)Write a PASCAL statement to calculate the total amount charged for the order.

______

______

(e) Output the result.

The total amount is $2880.00 .

Thanks for your order, have a good day!

______

______

______

______

______