This paper covers the history and use of labels in programming languages, from the beginning of programming to the present day. In early programming languages, labels were used a lot and had to be in specified positions. In present day languages can be most any place and are not used as much to branch to, but are used with loops and switch statements. The rules for using and constructing labels still vary a lot among present languages.

 

Labels. 1

Label Design Issues. 2

Statement Labels in Various Languages. 2

GOTO Statements. 3

Placement of Labels on a Line. 3

Placement of Labels within the Program.. 4

Dead Code. 5

Early Labels: 6

Assigning Values to Labels. 7

FORTRAN Assigned goto Statement 7

Computed goto Statement 7

PL/I Label Variables. 7

PL/I Label Arrays. 8

COBOL Alter Verb. 9

Scope of Labels. 10

Namespace of Labels. 10

Conclusion. 11

Questions: 11

 

 

Labels

 

Creative Commons License
This work is licensed under a Creative Commons Attribution-No Derivative Works 3.0 United States License.

Copyright Dennie Van Tassel 2004.

Please send suggestions and comments to dvantassel@gavilan.edu

 

Labels are used to branch to using goto statements in C and wide variety of other languages. In Java labels are used to exit (break) a named loop, or skip (continue) iterations of a named loop. In BASIC and FORTRAN labels are used to indicate the boundaries of a block of code to be executed. The term labels is mostly universal across languages, except in COBOL, their labels are called paragraph names. COBOL uses paragraph names (labels) as a place to branch to using a goto statement, and paragraph names are used to group statements into blocks of code to be executed by PERFORM ( similar to a for loop) commands. The case-selector in switch statements is very similar to a label and used to branch to in some languages (C#).

 

Labels started as positive integers (i.e., goto 20) in early BASIC and FORTRAN or alphanumeric identifiers with rules for composition similar to variables in later languages. Both COBOL and C++ could use:

 

   goto errors

 

for a branch, but C++ programmers would not admit to such use.

 

Label Design Issues

Here are a few design issues for labels:

 

  • What characters are used for label names? Examples are only positive integers in early BASIC and FORTRAN or standard identifier names in later languages.
  • How are labels marked? Many languages terminate a label with a colon, where other languages require a label to start in particular positions (early BASIC, FORTRAN, and COBOL) on a line.
  • Can a label be a keyword or reserved word such as while or if? Since label namespace is often different that variable namespace, it is possible to use reserved or keywords for labels in some languages.
  • Can a label name be the same as a function name, such as sqrt?
  • Do labels have a scope like variables? Do labels need to be declared?
  • Can a label follow another label? Or does a statement need to follow a label?
  • Are label variables available? Can we assign a label to a label variable during execution and pass it to a function?

 

All of the answers to these questions are yes in some language. We will now cover some of these label design issues in this chapter.

Statement Labels in Various Languages

Here is a list of labels composition in several languages.

 

Language

Label Syntax

BASIC

100

QBasic, Visual Basic

start_here:

FORTRAN IV

100

FORTRAN 90

starthere:

ALGOL

100:  or  start_here:

Ada

<<start_here>>

COBOL

START-HERE.  or 100-START-HERE.

Java/C++/C

start_here:

 

Sample Labels by Language

Table x.1

 

Many languages use colons at the end of the label. COBOL uses a period to terminate their paragraph (label) names. One other oddity about COBOL paragraph names is that they can start with a digit (10-START). Ada is the most unusual enclosing the labels in two angle brackets. The use of a colon after labels often means that we can have a label and variable by the same name! C++ allows this, but Java decided to forbid it.

 

GOTO Statements

The infamous goto statement is used to do an unconditional branch to a label. In our wild programming youth we used to do many of those, but with age we have reformed and do very few. Some languages such as FORTRAN and COBOL use the two word version go to, while other languages such as the C family use the single word version goto. But FORTRAN ignores spaces, so either version could be used. in FORTRAN.

 

Placement of Labels on a Line

There is some question where a label can be placed in a line. Labels are often placed at the beginning of a line. Besides making it easier to find a label by someone reading the program, there are historical reasons for this placement. Early languages (BASIC, FORTRAN, and COBOL) were line (card) oriented and very concerned with positions or columns on the line (card). Early FORTRAN and BASIC use positive integers at the beginning of the line. These labels cannot be elsewhere in the line. See examples below in this section. In modern languages labels can be even after a statement but are seldom used in this way. Here is where labels and statements must be in three early languages.

 

Column #

1-5

6

7-72

73-80

 

Labels

Continuation indicator

Program statements

Sequence or program name

FORTRAN Statement Layout

Figure x.1

BASIC labels are also integers that must be placed at the beginning of all the lines. The rest of the line was used for commands.

 

COBOL used alphanumeric paragraph names (labels), which had to start within positions (columns) 8-11, but could and usually continued after position 11. A COBOL label ends with a period. COBOL statements start in position 12 or later, so it easy to distinguish labels from other items in a COBOL program. Thus in all three of these early languages the position of the label in the line was closely restricted.

Column #

1-6

7

8-11

12-72

73-80

 

Sequence

Continue

Labels must start here

Program statements

Program name

COBOL Statement Layout

Figure x.2

 

Early versions of these three languages (FORTRAN, BASIC, COBOL) are fixed source form – meaning that statements must be in specific positions.

 

In FORTRAN, COBOL, and BASIC the sequence positions were used to sequence number the program statements. Programs were written on cards and after you dropped your cards on the floor because you were so excited to get an answer to 4 place accuracy, you needed some way to put the cards back in order.

 

In modern languages we should be able to do the following:

 

   A = 0;   mylabel:  b = 2;

 

In this code snippet we have the label mylabel: between two statements on the same line. Not all languages will allow this rather odd code. For example, in VB .NET, labels must appear at the beginning of a line of source code (following its BASIC heritage) , so the above code would be incorrect.

Placement of Labels within the Program

Labels can go any place in a program in most languages. But some early languages require that only certain type of statements must follow. For example, in FORTRAN statements are divided into executable and non-executable statements. Executable statements specify some action to take during execution of the program. Examples of executable statements are assignment statements, branches, loops, and calculations, all which do something. Non-executable statements provide information to the compiler. Examples of non-executable statements are declaring arrays or types of variables. FORTRAN declarations such as the type statements INTEGER and REAL cannot have labels. Likewise, array declarations, DATA statements, and the END statement cannot have a label. This last restriction appears in other languages. For example, we cannot have the following FORTRAN code:

 

       goto 190

       . . .

       print *, “All done.”

 190   end

 

In this code the integer 190 is a FORTRAN label. Instead we would need to change it to something like this:

 

       goto 190

       . . .

 190   print *, “All done.”

       end

 

FORTRAN IV needs an executable statement after a label and end is not an executable statement. Later versions of FORTRAN allow branches to end statements.

 

Other languages have similar requirements. For example, C needs a statement after a label. Here is an example, in the C family:

 

       goto done;

       . . .

  done: ;  /* null statement here. */

       }

 

In this snippet of code we use a null statement after the label done, since a statement must follow labels.

 

A related restriction is whether two or more labels can be used next to each other. For example, can we have something similar to the following:

 

  error1: error2:  cout << “input or output error” << endl;

 

In this interesting code snippet we have two consecutive labels error1 and error2. Some languages object to this.

Dead Code

Dead code are variables that are declared but never used, functions that are never called, or code that is skipped because of a branch. Since the dead code is not executed, it is an error, often a logic error. Dead code is created by careless programmers. An easy way to create dead code is by use of a goto statement and labels. Here is an example:

 

   goto processing;

   a = a + 1;  // dead code here.

 

Since we have an unconditional branch to the label processing on the first line, the assignment statement on the second line can never be executed. A return statement can be used to make dead code in Java and most other languages. Here is an example:

 

   return;

   x = x = 1;  // dead code here.

 

We can create dead code a little more subtly in COBOL as follows:

 

    IF RATE IS GREATER THAN MINIMUM THEN

       GO TO UNDER-RATES

    ELSE GO TO OVER-RATES.

    IF RATE IS EQUAL TO MINIMUM THEN

       GO TO MATCHED-RATES.

 

So let’s see how awake you are! Look at the above code again. Are we going to three different places depending on whether RATE is less than, equal, or greater than MINIMUM? Actually, the second IF statement is another example of dead code. The program either branches to UNDER-RATES or OVER-RATES in the first IF statement, but never gets to the EQUAL test statement!

 

Early Labels:

FORTRAN programmers only put labels on lines needed for reference. The FORTRAN DO loop is similar to the for loop in modern languages. Here is a FORTRAN example:

 

       DO 10, K = 1, 10, 1

          PRINT *, K, 1.0/K

 10    CONTINUE

 

The “DO 10 . . .” indicates the start of the loop and the “10 CONTINUE indicates the end of the loop. I have taken a few liberties with the early FORTRAN syntax of the PRINT command to make it readable for you. FORTRAN IV labels must be in the first 5 positions of the line, and any place within those columns. The labels must be unsigned integers and 5 or fewer digits. The rest of the FORTRAN statements must be in positions or columns 7-72. Labels did not have to be in any order but FORTRAN programmers usually tried to use small numbers near the beginning of the program and large numbers near the end of the program. Thus when looking for the label 10 we would expect (hope) it was near the beginning of the program and label 4200 was near the end of the program, or a long way from the beginning.

 

FORTRAN used statement labels to indicate the end of DO loops, as above, but also a line to branch to as follows:

 

       DO 10, K=1, 11, 1

          M = K - 6

          IF (M .EQ. 0) GOTO 10

          PRINT *, M, 1.0/M

 10    CONTINUE

 

Two uses of labels are illustrated. The number 10 is used to indicate the last line of the DO loop, and the label is used as place to branch to with a goto statement. The above code will not print the inverse of zero, since that is a difficult inverse to calculate.

 

Early BASIC numbered all the lines, often by 10s so lines could be inserted later. So here is the same code in BASIC:

 

    210 FOR K = 1 TO 11 STEP 1

    220    M = K – 6

    230    IF (M = 0) THEN GOTO 250

    240    PRINT M, 1.0/M

    250 NEXT K

   

 

All lines were numbered and the lines were sequence checked by the BASIC interpreter. Thus in BASIC the line numbers had two purposes: 1) used for labels; 2) used to keep program in order. Later versions of BASIC added alphanumeric labels, which are covered later in this section.

 

Assigning Values to Labels

A few languages allow us to assign values to a label similar to how a variable is assigned a value. This programming flexibility means when reading a program we cannot tell where the program is branching. Here is how a few languages handle this.

 

FORTRAN Assigned goto Statement

We can use a label variable to modify the destination of the goto statement. Here is a FORTRAN example using an assigned goto statement: