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 'IF-THEN-ELSE': 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 12345 OUTPUT ABC ABC 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 3 9 2 -7 0 6 OUTPUT (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 1 OUTPUT 2 3 4 (b) Rewrite the statements in between CASE-END with the IF-THEN-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 1 OUTPUT 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 1 - 50 51 - 100 101 - 200 More than 200 Unit Price $25.00 $24.50 $24.00 $23.00 Write a program on the program sheet provided to do ALL of the followings: (a) Declare variables of suitable types: Variable name num_bat unit_price amount Meaning the number of batteries to be ordered the corresponding unit price the 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! _______________________________________________________________________________ _______________________________________________________________________________ _______________________________________________________________________________ _______________________________________________________________________________ _______________________________________________________________________________