Click here for ObjectSpace: Business- to- Business Integration Company
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


3-8 Turbo Pascal Features: EXIT, CASE-ELSE

If you entered an illegal value in program 3-9, such as the number 13 (as the month number), you simply get the message:

    There are 0 days in this month.

In order to handle the invalid data you have to use a suitable IF statement. In Turbo Pascal you can add an ELSE part to the control structure CASE in order to handle data that do not belong to any of the case labels. The CASE structure will then take the form:

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

Another feature of Turbo Pascal is the EXIT statement, which ends the execution of the program at any point. The EXIT statement is classified as an unconditional branching statement. In the following program these two features are illustrated. If you enter any number rather than the numbers from “1” to “12,” the ELSE part and the EXIT statement will end the program.

{ ------------------- figure 3-11 ------------------- }
PROGRAM DaysOfMonth2(INPUT,OUTPUT);
LABEL
 Start;
VAR
 Days, Month, Year:INTEGER;
BEGIN
Start:
 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;
 ELSE
   EXIT             { all other cases }
 END;
   WRITELN('There are ',Days,' days in this month.');
 GOTO Start
END.

This is a sample run:

Please enter the number of the month:1
There are 31 days in this month.
Please enter the number of the month:4
There are 30 days in this month.
Please enter the number of the month:13  ----> Exit the program

Summary

In this chapter you learned the branching control structures that help you to handle decisions in your program.

1.  You are now familiar with the simple IF-THEN statement used with simple decisions. It takes the form:
    IF condition THEN
          statement;
2.  You also know the complete IF-THEN-ELSE construct that contains the result and the alternative result:
    IF condition THEN
          statement
    ELSE
          statement;
3.  You also know how to handle complicated conditions using the ELSE-IF ladder in the form:
    IF condition-1 THEN
          statement-1
    ELSE IF condition-2
          statement-2
    ELSE IF condition-3
          statement-3
    ...
    ELSE
          statement-n;
4.  An alternative to the ladder is nesting the IF-THEN-ELSE constructs inside each other in the form:
    IF condition-1 THEN
          IF condition-2 THEN
                ...
                IF condition-n THEN
                  statement-n1
                ELSE
                  statement-n2
                ...
          ELSE
           statement-2
    ELSE
     statement-1;
5.  You learned how to use the multiple choice construct CASE, which is ready to handle many cases in the form:
    CASE expression OF
         label-1 : statement-1;
         label-2 : statement-2;
         ...
         label-n : statement-n;
    END
6.  In Turbo Pascal the CASE construct has more features, as it may contain the ELSE part which handles all the other cases that do not correspond to a label. It takes the form:
    CASE expression OF
         label-1 : statement-1;
         label-2 : statement-2;
         ...
         label-n : statement-n;
    ELSE
         statement
    END

You also understand that in any of the above formulas you can replace one statement by a block of statements using the BEGIN-END blocks.
7.  You were introduced as well to the unconditional branching statement GOTO which transfers the program control to a labeled statement. It takes the form:
    GOTO label;

The label in standard Pascal is a positive integer of up to 4 digits, while in Turbo Pascal it can be a valid identifier, or it may even begin with a number. You also know how to declare a label at the beginning of the declaration part of the program. In Turbo Pascal the LABEL section does not need to be the first section.
8.  Finally, you met the Turbo Pascal statement EXIT, which terminates the program at any point.

In the next chapter, you continue the control structures to learn how to build structured loops.


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.