To access the contents, click the chapter and section titles.
Learn Pascal in a Three Days (2nd Ed.)
(Publisher: Wordware Publishing, Inc.)
Author(s):
ISBN: 1556225679
Publication Date: 07/01/97
Chapter 3 Decisions
3-1 Making Decisions
So far, each of the programs in this book has been a series of instructions executed sequentially, one after the other. In real-life applications, however, you will usually need to change the sequence of execution according to specified conditions. Sometimes you need to use a simple condition like:
If it is cold then put your coat on.
In this statement the resultant action is taken if the condition is evaluated as TRUE (the weather is cold). If, however, the weather is fine, the whole statement is skipped. Some conditions can be multiple, like those in the following conversation:
Ok then, if I come back early from work, Ill see you tonight; else if it is too late Ill make it tomorrow; else if my brother arrives tomorrow we can get together on tuesday; else if tuesday is a holiday then let it be wednesday; else Ill call you to arrange for the next meeting!
Actually, your program can easily handle such chained or nested conditions as long as you write the adequate code.
In Pascal there are two control structures used to handle conditions and their resultant decisions: the binary choice construct if-then-else, and the multiple choice construct case.
3-2 The Simple Decision: IF-THEN
To express a simple condition you can use the IF-THEN statement, as in the following example:
IF Age < 18 THEN
WRITELN('Sorry, this is underage.');
The statement starts with the keyword IF, followed by a boolean expression (the condition to be tested), followed by the keyword THEN, followed by the result to be executed if the condition is TRUE (the WRITELN statement). As you can see, the IF construct is one statement ending with a semicolon. If the value of variable Age is less than 18, the part after the keyword THEN is executed; otherwise, the whole statement is skipped, and the program execution resumes its original flow at the next statement. This type of program control is called conditional branching. The IF-THEN statement takes the general form:
IF condition THEN
statement;
The construct is written in two lines just for readability, but it is one statement ending with a semicolon, and there is no obligation to leave extra spaces. You only need to separate the keywords (such as IF and THEN) from the rest of the statement by at least one space.
Example: Pascal Credit Card
Take a look at the following program, where a credit card limit is tested for a certain purchase. The program starts with declaration of the constant Limit which represents the credit card limit ($1000), and the variable Amount whose value will be received from the keyboard. The program displays the message Your charge is accepted if the Amount is less than or equal to the Limit. If the condition is FALSE the program will end without response.
{ ------------------------------ figure 3-1 ------------------------------ }
PROGRAM SimpleDecision(INPUT,OUTPUT);
CONST
Limit = 1000;
VAR
Amount:REAL;
BEGIN
WRITE('Please enter the amount:');
READLN(Amount);
IF Amount <= Limit THEN
WRITELN('Your charge is accepted.'); {End of the IF statement}
WRITELN('Press ENTER to continue..');
READLN
END.
A READLN statement is used to pause the screen while displaying the message Press ENTER to continue. Because this statement is outside the IF statement it will be executed whether the condition is TRUE or FALSE. Here are two sample runs:
Run 1:
Please enter the amount:200
Your charge is accepted.
Press ENTER to continue..
Run 2:
Please enter the amount:2000
Press ENTER to continue..
You can use two conditional statements to represent the two cases, the TRUE and the FALSE. In the following program another IF statement is added to deal with the other case (the amount is greater than 1000). The message The amount exceeds your credit limit is displayed in this case.
{ ------------------------------ figure 3-2 ------------------------------ }
PROGRAM TwoConditions(INPUT,OUTPUT);
CONST
Limit = 1000;
VAR
Amount:REAL;
BEGIN
WRITE('Please enter the amount:');
READLN(Amount);
IF Amount <= Limit THEN
WRITELN('Your charge is accepted.');
IF Amount > Limit THEN
WRITELN('The amount exceeds your credit limit.');
WRITELN('Thank you for using Pascal credit card.');
WRITELN('Press ENTER to continue..');
READLN
END.
The following are two sample runs of the program:
Run 1:
Please enter the amount:150
Your charge is accepted.
Thank you for using Pascal credit card.
Press ENTER to continue..
Run 2:
Please enter the amount:1500
The amount exceeds your credit limit.
Thank you for using Pascal credit card.
Press ENTER to continue..
As before, note that the last two lines were displayed in each case, as they do not belong to the conditional statements.
|