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


Tips on the IF-ELSE Puzzles

Nesting the IF constructs inside each other may become confusing (to the programmer), as one may not be able to tell which ELSE belongs to which IF. Look at this simple example:

    IF X >= 1 THEN
    IF y >= 18 THEN
    WRITELN('statement#1.')
    ELSE
    WRITELN('statement#2');

The rule is that each ELSE belongs to the last IF in the same block. This means that, in this example, the ELSE belongs to the second IF. Arranging the text with the proper indentation, according to this rule, makes it clearer:

    IF X >= 1 THEN
       IF y >= 18 THEN
         WRITELN('statement#1.')
       ELSE
         WRITELN('statement#2');

If, however, you want to associate ELSE with the first IF, you can use blocks as follows:

    IF X >= 1 THEN
       BEGIN
         IF Y >= 18 THEN
            WRITELN('statement#1.')
       END
    ELSE
       WRITELN('statement#2');

Drill 3-3

Write a program to describe the weather according to the following temperature classifications:

Temperature Classification
greater than 75 hot
50 to 75 cool
35 to 49 cold
less than 35 freezing

3-6 The Multiple Choice: CASE

The CASE construct is used to deal with multiple alternatives, such as the user-menu options. It takes the general form:

    CASE expression OF
         label-1 : statement-1;
         label-2 : statement-2;
         ...
         label-n : statement-n;
    END

The case expression (also called the selector) can be of any type except REAL. According to the value of this expression the control of the program is transferred to one of the case labels, and the corresponding statement is executed. The labels actually represent the different possible values of the expression. Look at this example:

Example: A Vending Machine

The coins in the vending machine are sorted according to the weight of each coin, which is assumed to be 35 grams for a quarter, 7 for a dime, and 15 for a nickel.

This logic can be programmed as follows:

    CASE CoinWeight OF
     35: Amount:= Quarter;
     7 : Amount:= Dime;
     15: Amount:= Nickel;
    END;

The numbers 35, 7, and 15 represent the “CoinWeight” and are used as labels. Therefore, when the “CoinWeight” equals 7, for example, the statement:

    Amount:= Dime;

is executed. Needless to say, the name “Dime” is a named constant whose value is 10, and “Nickel” and “Quarter” are named constants as well. Look at the complete program:

{ ------------------------------ figure 3-8 ------------------------------ }
PROGRAM CaseOfWeights(INPUT,OUTPUT);
CONST
 Quarter = 25;
 Dime = 10;
 Nickel = 5;
VAR
 CoinWeight, Amount:INTEGER;
BEGIN
 WRITE('Please enter the weight:');
 READLN(CoinWeight);
 CASE CoinWeight OF
  35: Amount:= Quarter;
  7 : Amount:= Dime;
  15: Amount:= Nickel;
 END;
 WRITELN('The amount is ', Amount, ' cents.');
 READLN
END.

This is a sample run of the program:

Please enter the weight:35  ----> Enter "35"
The amount is 25 cents.     ----> The program response

You can use more than one label for the same result statement, which will save a lot of writing as compared to the IF in the same situation. Here is an example:

Example: Number of Days in a Month

Consider, for instance, that you want to program a code that reads the number of the month and tells the number of days in that month. The CASE construct will look something like the following:

    CASE Month OF
     1,3,5,7,8,10,12    : Days:= 31;
     4,6,9,11           : Days:= 30;
     2                  : Days:= 28;
    END;

As you can see, the CASE construct here contains three cases, two of them with more than one label. All months that have 31 days belong to the first case, those that have 30 days belong to the second case, and February is a special case by itself. We assume here that February has 28 days for simplicity, but you can extend the logic to determine if the year is a leap year and assign February a value of 29 or 28 accordingly. You may use a block of statements for one case like this:

    CASE Month OF
     1,3,5,7,8,10,12 : Days:= 31;
     4,6,9,11     : Days:= 30;
     2          : BEGIN
                   WRITE('Enter the year:');
                   READLN(Year);
                   IF YEAR MOD 4 = 0 THEN
                      Days:=29
                   ELSE
                      Days:=28
                  END;

Here the case label “2” leads to a block of statements. So, if you enter “2” as the number of the month, the program will ask you to enter the year. The year will be tested and you will get 29 if the year is a leap year and 28 otherwise. Here is the complete program:

{ ------------------------------ figure 3-9 ----------------------------- }
PROGRAM DaysOfMonth1(INPUT,OUTPUT);
VAR
 Days, Month, Year:INTEGER;
BEGIN
 WRITE('Please enter the number of the month:');
 READLN(Month);
 CASE Month OF
  1,3,5,7,8,10,12 : Days:= 31;
  4,6,9,11     : Days:= 30;
  2          : BEGIN
                WRITE('Enter the year:');
                READLN(Year);
                IF YEAR MOD 4 = 0 THEN
                   Days:=29
                ELSE
                   Days:=28
               END;
 END;
  WRITELN('There are ',Days,' days in this month.');
 READLN
END.

The following are sample runs of the program:

Run 1:

Please enter the number of the month:2
Enter the year:1987
There are 28 days in this month.

Run 2:

Please enter the number of the month:2
Enter the year:1984
There are 29 days in this month.

Run 3:

Please enter the number of the month:12
There are 31 days in this month.


NOTE:   Notice that the leap year test in this program is a simplified logic, useful for the years of one century. The complete logic of the leap year definition is:
  The year is divisible by 4 AND not divisible by 100 OR,
  The year is divisible by 400.
You may expand the logic as a drill.

In cases like this, using the CASE construct is more efficient than using nested IF-THEN-ELSE constructs or ladders. However, you must have realized that you will sometimes need them both (as in the February case).


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.