|
To access the contents, click the chapter and section titles.
Learn Pascal in a Three Days (2nd Ed.)
Using BlocksIf you want to use more than one statement as a result of one condition, you can use the BEGIN-END blocks. You can actually use any number of blocks inside the program main body, using BEGIN and END to mark the territories of each block. A block will be treated as one unit, no matter how many statements it includes. Look at the following example: { ------------------------------ figure 3-3 ------------------------------ } PROGRAM UsingBlocks(INPUT,OUTPUT); CONST Limit = 1000; VAR Amount:REAL; BEGIN WRITE('Please enter the amount:'); READLN(Amount); IF Amount <= Limit THEN BEGIN WRITELN('Your charge is accepted.'); WRITELN('Your price plus tax is $',1.05*Amount:0:2) { The semicolon is optional } END; IF Amount > Limit THEN BEGIN WRITELN('The amount exceeds your credit limit.') ; WRITELN('The maximum limit is $',Limit) { The semicolon is optional } END; WRITELN('Thank you for using Pascal credit card.'); WRITELN('Press ENTER to continue..'); READLN { The semicolon is optional } END. In this example more than one statement is executed in either case (TRUE or FALSE). For this reason two blocks were used.
Here are two sample runs: Run 1: Please enter the amount:120 Your charge is accepted. Your price plus tax is $126.00 Thank you for using Pascal credit card. Press ENTER to continue.. Run 2: Please enter the amount:2000 The amount exceeds your credit limit. The maximum limit is $1000 Thank you for using Pascal credit card. Press ENTER to continue.. If you try the program without the blocks, you will find that only the first statement that follows the keyword THEN belongs to the IF statement, but any other statement belongs to the main program and will be executed regardless of the condition. 3-3 The IF-ELSE-THEN ConstructThe form you have used so far for the IF statement is actually a simplified version of the complete construct. The complete IF statement includes the two cases that result from testing the condition. It takes the form: IF condition THEN statement ELSE statement; Notice here that only one semicolon is used, because the whole construct is treated as one statement. Here is an example: IF AGE < 18 THEN WRITELN('Underage.') ELSE WRITELN('Age is OK.'); This statement will display the message Underage if Age is less than 18. In the other case the message Age is OK is displayed. If you add another statement to either of the two cases, you have to use the BEGIN-END blocks. The new construct will look like this: IF AGE < 18 THEN BEGIN WRITELN('Underage.'); WRITELN('Wait another couple of years.') END { No semicolon is used here } ELSE BEGIN WRITELN('Age is OK.'); WRITELN('You don''t have to wait.') END; { A semicolon is mandatory here }
Now, back to the Pascal credit card program to enhance it with the complete IF-THEN-ELSE statement. { ------------------------------ figure 3-4 ------------------------------ } PROGRAM CreditCard(INPUT,OUTPUT); CONST Limit = 1000; VAR Amount:REAL; BEGIN WRITE('Please enter the amount:'); READLN(Amount); { Beginning of the IF construct } { ----------------------------- } IF Amount <= Limit THEN BEGIN WRITELN('Your charge is accepted.'); WRITELN('Your price plus tax is $',1.05*Amount:0:2) END ELSE BEGIN WRITELN('The amount exceeds your credit limit.'); WRITELN('The maximum limit is $',Limit) END; { End of the IF construct } { ----------------------- } WRITELN('Thank you for using Pascal credit card.'); WRITELN('Press ENTER to continue..'); READLN END. Sample runs of the program give the following results: Run 1: Please enter the amount:1000 Your charge is accepted. Your price plus tax is $1050.00 Thank you for using Pascal credit card. Press ENTER to continue.. Run 2: Please enter the amount:1001 The amount exceeds your credit limit. The maximum limit is $1000 Thank you for using Pascal credit card. Press ENTER to continue..
3-4 The ELSE-IF LaddersAlthough the IF-THEN-ELSE statement is intended for binary choice, it can be extended to handle more complicated choices. Look at this new arrangement of the construct, which is sometimes referred to as the ELSE-IF ladder: IF condition-1 THEN statement-1 ELSE IF condition-2 statement-2 ELSE IF condition-3 statement-3 ... ELSE statement-n; The conditions in the ladder are evaluated from the top down, and whenever a condition is evaluated as TRUE, the corresponding statement is executed and the rest of the construct is skipped. If no condition has been satisfied, the last ELSE will be brought into action. Notice that the condition ladder is considered one statement ending with a semicolon, but no semicolons are used inside. If you want to use more than one result-statement, you have to use the BEGIN-END blocks according to the rules mentioned earlier.
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |