home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
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

Bookmark It

Search this book:
 
Previous Table of Contents Next


Example: A Character Tester

This program starts by asking you to enter a letter, then tests the input character to see if it is a lowercase or uppercase letter. The program can also recognize numbers and deliver an appropriate message, but otherwise it displays: “Sorry, this is not a letter.”

The logic used in the program depends on testing the ASCII code of the input characters using the ORD function. The characters are classified as follows:

  The uppercase letters correspond to the codes from 65 to 90.
  The lowercase letters correspond to the codes from 97 to 122.
  The digits correspond to the codes from 48 to 57.

If you already wrote this program as a solution to Drill 3-1, you will find that the ELSE-IF ladder makes things easier.

{ ------------------------------ figure 3-5 ------------------------------ }
PROGRAM CharsTester(INPUT,OUTPUT);
VAR
 InputChar:CHAR;
BEGIN
 WRITE('Please enter an alphabetic character:');
 READLN(InputChar);
{ Beginning of the IF construct }
{ ----------------------------- }
 IF (ORD(InputChar) > 64) AND (ORD(InputChar) < 91) THEN
   WRITELN('This is an uppercase letter.')
 ELSE IF (ORD(InputChar) > 96) AND (ORD(InputChar) < 123)THEN
   WRITELN('This is a lowercase letter.')
 ELSE IF (ORD(InputChar) > 47) AND (ORD(InputChar) < 58) THEN
   WRITELN('Hey, this is a number!')
 ELSE
   WRITELN('Sorry, this is not a letter.');
{ End of the IF construct }
{ ----------------------- }
 WRITELN('Press ENTER to continue..');
 READLN
END.

The following are four sample runs for four different inputs:

Run 1:

Please enter an alphabetic character:a  ----> Enter "a"
This is a lowercase letter.
Press ENTER to continue..

Run 2:

Please enter an alphabetic character:B  ----> Enter "B"
This is an uppercase letter.
Press ENTER to continue..

Run 3:

Please enter an alphabetic character:5  ----> Enter "5"
Hey, this is a number!
Press ENTER to continue..

RUN 4:

Please enter an alphabetic character:@  ----> Enter "@"
Sorry, this is not a letter.
Press ENTER to continue..

3-5 Nested Conditions

The statement to be executed upon testing a condition can be of any kind. As a matter of fact, it can be another IF statement nested in the original IF statement.

The IF-THEN-ELSE constructs can be nested inside each other, as in the following form:

    IF condition-1 THEN
          IF condition-2 THEN
                ...
                IF condition-n THEN
                  statement-n1
                ELSE
                  statement-n2
                ...
          ELSE
           statement-2
    ELSE
     statement-1;

As you can see, this construct can handle any number of nested conditions, but you have to keep track of each IF and the corresponding ELSE. Let us put the construct into action.

Example: Scores and Grades

This program receives the score of a student and displays the grade according to the following classification:

1.  Grade “A” corresponds to scores from 90% to 100%.
2.  Grade “B” corresponds to scores from 80% to 89%.
3.  Grade “C” corresponds to scores from 70% to 79%.
4.  Grade “D” corresponds to scores from 60% to 69%.
5.  Grade “F” corresponds to scores less than 60%.

Here is the program:

{ ------------------------------ figure 3-6 ------------------------------ }
PROGRAM ScoresAndGrades1(INPUT,OUTPUT);
VAR
 Score:INTEGER;
BEGIN
 WRITE('Please enter the score:');
 READLN(Score);
 WRITELN;
{ Beginning of the IF construct }
{ ----------------------------- }
 IF Score > 59 THEN
   IF Score > 69 THEN
     IF Score > 79 THEN
       IF Score > 89 THEN
         WRITELN('Excellent. Your grade is ''A''')
       ELSE
         WRITELN('Very good. Your grade is ''B''')
     ELSE
       WRITELN('Good. Your grade is ''C''')
   ELSE
     WRITELN('Passed. Your grade is ''D''')
 ELSE
   WRITELN('Better luck next time. Your grade is ''F''');
{ End of the IF construct }
{ ----------------------- }
 WRITELN('Press ENTER to continue..');
 READLN
END.

The following are sample runs of the program:

Run 1:

Please enter the score:92        ----> Enter "92"
Excellent. Your grade is 'A'     ----> The program response
Press ENTER to continue..

Run 2:

Please enter the score:70
Good. Your grade is 'C'
Press ENTER to continue..

Run 3:

Please enter the score:60
Passed. Your grade is 'D'
Press ENTER to continue..

Run 4:

Please enter the score:59
Better luck next time. Your grade is 'F'
Press ENTER to continue..

As usual, you may cause more than one result statement to be executed upon testing a condition by embedding the statements into a block.

You can use any one of the available variations of the IF-THEN-ELSE construct in your applications. However, some forms are more reliable with one application, and some with others. Look at this program, which processes the same problem of the “scores and grades” but uses the ELSE-IF ladder. Notice how the program is made easier and more comprehensible to the reader by using the boolean variables “A,” “B,” “C,” “D,” “F.” Note also that illegal numbers are filtered out by the last ELSE.

{ ------------------------------ figure 3-7 ------------------------------ }
PROGRAM ScoresAndGrades2(INPUT,OUTPUT);
VAR
 Score       :INTEGER;
 A, B, C, D, F:BOOLEAN;
BEGIN
 WRITE('Please enter the score:');
 READLN(Score);
 A:= (Score >= 90) AND (Score <= 100);
 B:= (Score >= 80) AND (Score < 90);
 C:= (Score >= 70) AND (Score < 80);
 D:= (Score >= 60) AND (Score < 70);
 F:= (Score < 60) AND (Score >= 0);
 WRITELN;
{ Beginning of the IF construct }
{ ----------------------------- }
 IF A THEN
   WRITELN('Excellent. Your grade is ''A''')
 ELSE IF B THEN
   WRITELN('Very good. Your grade is ''B''')
 ELSE IF C THEN
   WRITELN('Good. Your grade is ''C''')
 ELSE IF D THEN
   WRITELN('Passed. Your grade is ''D''')
 ELSE IF F THEN
   WRITELN('Better luck next time. Your grade is ''F''')
 ELSE
   WRITELN('This number is out of range.');
{ End of the IF construct }
{ ----------------------- }
 WRITELN('Press ENTER to continue..');
 READLN
END.


Previous Table of Contents Next


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.