Jump Statements in Turbo Pascal

November 1996

NOTE: All of this material applies to Turbo Pascal version 7.0, but some of it may not apply to earlier versions. The version in the Engineering Center labs is 6.x.


Overview

Turbo Pascal includes several commands for jumping out of a loop or a block of code before it would normally finish. These are a bit like the dreaded GOTO, but they're more acceptable because they jump to a clearly defined point in the structured code.

These commands are not part of the standard, official Pascal definition. Like many other features, they were added to Turbo Pascal because they had been found useful in the C language.

break

The Break statement causes the program to jump out of a loop and go to the statement following the loop. See Savitch, pp. A45-46, for examples.

continue

The Continue statement causes the program to jump out of the middle of a loop and continue the same loop beginning with the boolean test that controls it (While, Until, or For).

exit

The Exit statement is similar to Break, except it causes the program to jump out of the current "block" -- either a procedure, a function, or a program. If it was a program, the program stops. See Savitch, pp. A1-3 for more information.

halt

The Halt statement stops the program. It can also return a number telling DOS (or some other program) whether it ran succesfully or not.

When to Use Them

The Break and Continue commands let you put a boolean test into the middle of a loop. That means you can do something like read a line of data, test to make sure it's OK, and based on that test...

The Exit command can be used like the Break command, except it jumps out of a function or procedure. (With a function, be sure you've set the return value first.) The Halt command can sometimes be useful in debugging, when you want the program to run up to a certain point and then stop before it tries to execute a section that is buggy. It's also used by programs that want to report their exit status to other programs or the user-interface shell.

What Do They Add to the Language?

These commands don't let you do anything that you couldn't have done with if-then-else statements. But they sometimes make a program more readable.

Typically they're used when there is a single, special exception to the usual behavior of a loop (or block), and it is impossible to test for the exception at the beginning or end of the loop. If you didn't have these commands, you would have to write "if not theException then [do the rest of the loop]". But that would make most of the loop body look like a special case. It can be more readable to make that usual behavior the loop body, and include a statement of the form, "if theException then break" to handle the special case.


RETURN to CSCI 1200 Home Page

- John Rieman