|
To access the contents, click the chapter and section titles.
Learn Pascal in a Three Days (2nd Ed.)
3-7 Unconditional Branching: GOTOThe GOTO statement is used to transfer control of the program from one point to another. It is classified as unconditional branching. Although the GOTO statement is very easy to use, you rarely see it in Pascal programs because it destroys the structure of the program. In some cases, however, it may be useful in escaping from many levels of nesting in one jump. The syntax of the GOTO statement is as follows: GOTO label; The label is a positive integer of up to four digits preceding the required statement (in Turbo Pascal the label can be any valid identifier and may begin with a digit). GOTO 1000; ... 1000: WRITELN('I am a labeled statement.'); ... When the GOTO is encountered, the program control is transferred to the labeled statement. The label must be declared in the label section of the declaration part of the program. The label section starts with the keyword LABEL and comes as the first section in the declaration part in standard Pascal (in Turbo Pascal there is no such obligation). Look at this example: PROGRAM GoToDemo(INPUT,OUTPUT); LABEL 1000; VAR InputChar:CHAR; BEGIN WRITE('Please enter a letter (or 0 to quit):'); READLN(InputChar); IF InputChar = '0' THEN GOTO 1000; { Other statements may go here... } 1000: END. In this example, the value of the input character is tested to see if it is zero, in which case control is transferred to the part following the label 1000, which is the end of the program. If you are using Turbo Pascal, you can use meaningful labels such as Wrapup or Start instead of the numbers. Repetition LoopsYou can use the GOTO statement to build a closed loop. For example, if you want to repeat the execution of the character tester program you may use the following logic, where the control is always transferred to the label 1000 at the beginning of the program. A condition is used to end the loop (and the program) by examining the input value. If a zero is entered, the control is transferred to the label 2000, ending the program. If you remove this condition from the program, it will be repeated infinitely. The only way to exit the program in this case is to use the control keys Ctrl-Break. This kind of loop is called an infinite loop. { ------------------- figure 3-10 ------------------- } PROGRAM CharsTester2(INPUT,OUTPUT); LABEL 1000, 2000; { label declaration } VAR InputChar:CHAR; BEGIN 1000: WRITE('Please enter a letter (or 0 to quit): '); READLN(InputChar); { Beginning of the IF construct } { ----------------------------- } IF InputChar = '0' THEN { a condition to exit } GOTO 2000 ELSE 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 } { ----------------------- } GOTO 1000; { restart the program } 2000: { exit the program } END. The following is a sample run of the program: Please enter a letter (or 0 to quit):W ----> Enter "W" This is an uppercase letter. Please enter a letter (or 0 to quit):e ----> Enter "e" This is a lowercase letter. Please enter a letter (or 0 to quit):0 ----> Enter "0" This method, as you can see, is not the best method with which to build loops or control program execution, as it consists of jumps from one point to another. In the next chapter you are introduced to Pascal 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. |