FORTRAN,
BASIC, and COBOL STOP Statements
Assembly
Language nop Instruction
Parentheses
around Comparisons
Copyright Dennie Van Tassel 2004.
Please send suggestions and comments to dvantassel@gavilan.edu
There are two ends to a program: the physical end and logical end. The physical end is the last statement in the program. The logical end is where the program stops execution. In C language family the final brace } is used to end the program. For example in C++ we have:
main() {
. . .
} // physical end of the program.
One
problem (or advantage) with this approach is that all kinds of blocks and compound
statements use the same symbol for ending. Other languages use a particular
statement (e.g. END) for ending the program that is used in no
other place. In these languages we know when we see an END statement, we at ending the
program and not just ending a compound statement or
block.
Other
languages have several ways to terminate program execution. For example, PL/I has four ways: end, stop,
exit, and return. The end statement is the last line
of the program or function. The stop or return statements are used for
normal termination and exit is used
for abnormal termination. Return statements are covered a little in this chapter
but are covered more in the Function chapter.
Most
early FORTRAN or BASIC programs would have an ending similar to this:
10 STOP
END
The
END statement is the last line
in the main program and in all subroutines or functions. Since an END statement is a non-executable
statement, the program cannot branch to an END statement. (This problem is discussed in detail in the “Placement of Labels within the Program”
section of the Labels chapter.) The STOP statement is the logical end of the
main program. And the statement number 10 on the STOP statement can be branched to as needed.
ALGOL
used the same stop and end statements as used in
FORTRAN. But ALGOL has blocks and uses begin and end for the block structure. So
the begin and end statements of ALGOL are
equivalent to open and closing brace in the C family of
languages. Thus the ALGOL end is used to indicate the end
of compound statements and the end of the program like the C family does with
closing braces.
Pascal
uses the end statement to indicate the end of compound statements and the program.
But the final end statement in Pascal must be followed by a period. So “end” is the end of a
compound statement or block, but “end.” (end period) is end of the program. Thus Pascal has a special command to end the
program.
COBOL
does not have an end statement. The last statement is the last statement and
that is all there is to it. When looking at other languages this seems a little
odd but it has worked fine with millions of COBOL programs.
Ruby
programmers use “__END__” (two underscores before and after END) with no leading or trailing white space, to end
their programs and any statements that follow will not be compiled. Ruby and
Awk have END blocks that are executed at
the close of the programs and these are discussed in the Block chapter.
So
some languages with a specific statement for the physical end of the program
are BASIC, FORTRAN, and Pascal. And I guess I have reached the end of this topic.
Some
languages use the end statement and the name of function,
program, or procedure to end the program. For example,
procedure swap(a, b: in out elem) is
temp: elem;
begin
temp := a;
a := b;
b := temp;
end swap;
So
the name of the procedure is swap and that is used to label the end statement.
FORTRAN
has programs, subroutines, and functions. In FORTRAN 90 each unit has their own
closing end statement, and the unit name can be optionally attached. For example,
we could have the following end statements:
end program main
! the program name is main
end function swap ! the function name is swap
end subroutine heading
! the subroutine name is heading
These
unit names are optional, but if there it must be
identical to the name specified in the program, function, or subroutine statement. In FORTRAN, functions
return a single value via the name of the function, whereas subroutines return
values by the argument list. Visual Basic makes a similar use of end statements, indicating the
unit being ended.
PL/I
uses similar label end statements. Here is the
swap program in PL/I:
swap:
procedure(a, b); /* notice the semicolon */
declare a, b,
temp float;
temp = a;
a = b;
b = temp;
end swap;
Again
the procedure name swap is used for the name of the
procedure and for the final end statement. This method is used in PL/I for the main program, subroutines, and functions.
A
statement or something is needed to stop the execution of the program. This is
the logical end of the program. In C++ we could just
have:
main(){
cout
<< “Good bye world.” << endl;
} // logical end of the program.
In this great program, the last brace (some times called close curly brace) indicates the logical end of the program. This method of ending is called falling off the end and considered non-elegant. We could be more politically correct (PC) if we did the following:
int
main(){
cout
<< “Good bye world.” << endl;
return(0); // logical end of the program.
}
So in C++ and other similar languages, the return statement is used to stop the execution of the program when in main( ).
While
we can only have one physical end (e.g. end or closing brace) of the
program, we can have one or more logical ends or return statements. For example:
if (x < 0) return(1); // error end of the program.
. . .
cin
>> x;
. . .
return(0); // normal end of the program.
}
Now
we can stop the execution of the program with the return statement in the if
statement. Or we can stop the execution of the program with the return statement before the closing brace. When you get to
the section on Return Codes later in this chapter, I cover the values (1 or 0)
in the return statements.
The
problem with using a return statement to stop the
program is that we may want to stop the program while in a function or
subroutine. But if we are in a function, then the return statement will just get us
back to the main or calling program. This is why some languages have a separate
command to stop execution. C++ uses the exit() function to terminate the program execution. Here
is an example:
if (x < 0) exit(1);
The
exit() is a function and not a
command. Unlike the return statement, it will cause a
program to stop execution even in a function. And the exit() function can also return a value when executed, like the return statement. So the C family has
three ways to end the program: exit(), return, and final closing brace.
Both
FORTRAN and BASIC have a STOP statement. The STOP statement cannot return a
value like exit() in C++. There is only one END statement in the program,
but there can be several STOP statements. We may have something like this
in these languages:
IF (x < 0 ) STOP
. . .
STOP
END
Or
we might change the above to the following so there is only one STOP statement:
IF (x < 0) GOTO 150
. . .
150 STOP
END
Some
people would argue that one of those is better than the other but I will decline
getting involved in that argument. A STOP statement is customarily used
but not required, since the END statement will take care of it. Thus we can
fall off the end of the program (hit the END statement) and the program also
halts.
COBOL
uses STOP RUN to
terminate a program, which illustrates the notion that COBOL programmers will
always use more words if they can. Like its similar cousin in FORTRAN we can
have several STOP RUN statements and if we have
none, the program will still cease after processing the last statement.
FORTRAN
I allowed an octal integer after the STOP statement, as follows:
STOP 4
The
octal integer, called a return code, would display on the IBM 704 console in the address field of the
storage register and the programmer could see which STOP halted the program. This
was a LONG time before time-sharing. Then you could look at the console and see
what number was there and know why the program stopped. Only one program could
execute at a time, so that programmer got the machine and the console. It is
difficult to understand why a short print statement indicating the problem
might not be more useful. While this may nowadays seem like a quaint use for STOP statements, we can see how it has evolved into
similar uses.
This
use is more than a historical note, since IBM Job Control Language uses
condition codes and the C family of languages uses an
integer in their return statements to indicate the
success or failure of program execution.
The
custom in the C family of languages is to return a value zero if the program
executed successfully. Thus we use:
return(0);
A
non-zero value, indicates a problem or non-successful
program execution. Thus we could use something like this:
if (x < 0) return(1);
Different
integers could be used to indicate the type of error. This type of information
is useful, since often one program calls another program and the first program
needs to know if the second program successfully executed. For example, one
program could process and prepare data, and then request a sort program to sort
the data file. If the sort worked correctly, then a third program could be
called to print the data. These are all separate programs.
IBM Job Control Language (JCL) used condition code to indicate the relative success of the jobstep. A condition code of zero may indicate a successful
run. IBM designated certain condition code numbers that normally indicate some
kind of problem and should not be ignored. These numbers are in multiples of
four: 0004, 0008, 0012, 0016, 0020, and 0024. The different codes had a
suggested meaning. Codes 0001-0003 usually indicated successful run. Code 0004 (or
higher) was used for a possible error but execution was possible. Code 0008 (or
higher) is a serious error, and each higher number from the above list
indicates a more serious error.
PHP
has the exit and return commands that do the normal things, but also has
the die() function. The die() function will stop
executing the program and return the expression to the browser just before
aborting the script. Here is some PHP code:
$file = fopen($filename, ‘r’) or
die(“unable
to open $filename”);
This
die() function goes beyond an exit or return since it can return an
expression such as a character string before terminating the program.
Perl
has a similar die function that is also often
used in the right side of a logical or, as follows:
open(FILE,
“myfile”) or die ”Can’t open myfile\n;
The
output from die is sent to standard error
and the program is terminated. Perl has a less disruptive function called warn, which also sends messages
out to standard error, but the program does not terminate. While die is often used on the right side of an open command, it can be used many other places. The Perl die command also has access to the special variable $! which contains the most recent system
error message.
FORTRAN
had a PAUSE statement. It was used to halt the
computer in such a way that execution can be resumed. This was necessary so special
paper (paychecks) or magnetic tape could be loaded on the
machine for the next part of the job processing. One variation of the PAUSE
statement might look as follows
PAUSE 'MOUNT THE NEXT TAPE'
I
guess it is time to stop (0) or pause on this topic. I bet some
of you did not know all the ways to stop a program smoothly. In COBOL the JCL
was used to tell us to mount tape or change paper for the printer.
In this section on ending the
program, I first separate the logical end and the physical end of the program.
The physical end of the program is the last curly brace in many languages, but
other languages use a special command such as an end statement.
The logical end of the
program is the command that stops the execution of the program and this end can
be placed any place in the program. There is a wide variety of commands to
terminate program execution, including stop, exit, and return statements. And many languages can return an exit code
when stopping the program. Readers interested in the topics covered in this
section should also look at the return statement in the Function chapter.