|
To access the contents, click the chapter and section titles.
Learn Pascal in a Three Days (2nd Ed.)
3-8 Turbo Pascal Features: EXIT, CASE-ELSEIf 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 SummaryIn this chapter you learned the branching control structures that help you to handle decisions in your program.
In the next chapter, you continue the control structures to learn how to build structured loops.
|
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. |