In
programming language
A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language.
The description of a programming l ...
s, a label is a sequence of characters that identifies a location within
source code
In computing, source code, or simply code, is any collection of code, with or without comment (computer programming), comments, written using a human-readable programming language, usually as plain text. The source code of a Computer program, p ...
. In most languages, labels take the form of an
identifier
An identifier is a name that identifies (that is, labels the identity of) either a unique object or a unique ''class'' of objects, where the "object" or class may be an idea, physical countable object (or class thereof), or physical noncountable ...
, often followed by a
punctuation character (e.g., a
colon). In many
high-level languages, the purpose of a label is to act as the destination of a
GOTO
GoTo (goto, GOTO, GO TO or other case combinations, depending on the programming language) is a statement found in many computer programming languages. It performs a one-way transfer of control to another line of code; in contrast a function c ...
statement. In
assembly language, labels can be used anywhere an
address
An address is a collection of information, presented in a mostly fixed format, used to give the location of a building, apartment, or other structure or a plot of land, generally using political boundaries and street names as references, along ...
can (for example, as the operand of a
JMP
or
MOV
instruction). Also in
Pascal and its derived variations. Some languages, such as
Fortran and
BASIC
BASIC (Beginners' All-purpose Symbolic Instruction Code) is a family of general-purpose, high-level programming languages designed for ease of use. The original version was created by John G. Kemeny and Thomas E. Kurtz at Dartmouth College ...
, support numeric labels.
Labels are also used to identify an entry point into a
compiled sequence of statements (e.g., during
debugging
In computer programming and software development, debugging is the process of finding and resolving ''bugs'' (defects or problems that prevent correct operation) within computer programs, software, or systems.
Debugging tactics can involve in ...
).
C
In
C a label identifies a statement in the code. A single statement can have multiple labels. Labels just indicate locations in the code and reaching a label has no effect on the actual execution.
Function labels
Function labels consist of an identifier, followed by a colon. Each such label points to a statement in a function and its identifier must be unique within that function. Other functions may use the same name for a label. Label identifiers occupy their own namespace – one can have
variables and
functions with the same name as a label.
void foo(int number)
Here ''error'' is the label. The statement
goto
GoTo (goto, GOTO, GO TO or other case combinations, depending on the programming language) is a statement found in many computer programming languages. It performs a one-way transfer of control to another line of code; in contrast a function c ...
can be used to jump to a labeled statement in the code. After a
goto
, program execution continues with the statement after the label.
Switch labels
Two types of labels can be put in a switch statement. A case label consists of the keyword
case
, followed by an expression that evaluates to integer constant. A default label consists of the keyword
default
. Case labels are used to associate an integer value with a statement in the code. When a switch statement is reached, program execution continues with the statement after the case label with value that matches the value in the parentheses of the switch. If there is no such case label, but there is a default label, program execution continues with the statement after the default label. If there is no default label, program execution continues after the switch.
switch (die)
Within a single
switch statement
In computer programming languages, a switch statement is a type of selection control mechanism used to allow the value of a variable or expression to change the control flow of program execution via search and map.
Switch statements function som ...
, the integer constant associated with each case label must be unique. There may or may not be a default statement. There is no restriction on the order of the labels within a switch. The requirement that case labels values evaluate to integer constants gives the compiler more room for optimizations.
Examples
Javascript
In JavaScript language
syntax statements may be preceded by the label:
top: //Label the outermost for-loop.
for (var i = 0; i < 4; i++)
alert("i=" + i + ", j=" + j); //i=2, j=3
It also possible to use
break statement to break out of the code blocks:
top:
// Which would output:
// > foo
// > bar
Common Lisp
In
Common Lisp
Common Lisp (CL) is a dialect of the Lisp programming language, published in ANSI standard document ''ANSI INCITS 226-1994 (S20018)'' (formerly ''X3.226-1994 (R1999)''). The Common Lisp HyperSpec, a hyperlinked HTML version, has been derived fr ...
two ways of defining labels exist. The first one involves the
tagbody
special operator. Distinguishing its usage from many other programming languages that permit global navigation, such as
C, the labels are only accessible in the context of this operator. Inside of a
tagbody
labels are defined as forms starting with a symbol; the
go
special form permits a transfer of control between these labels.
(let ((iteration NIL))
(tagbody
start
(print 'started)
(setf iteration 0)
increase
(print iteration)
(incf iteration 1)
(go check)
check
(if (>= iteration 10)
(go end)
(go increase))
end
(print 'done)))
A second method utilizes the reader macros
#''n''=
and
#''n''#
, the former of which labels the object immediately following it, the latter refers to its evaluated value. Labels in this sense constitute rather an alternative to variables, with
#''n''=
declaring and initializing a “variable” and
#''n''#
accessing it. The placeholder ''n'' designates a chosen unsigned decimal integer identifying the label.
(progn
#1="hello"
(print #1#))
Apart from that, some forms permit or mandate the declaration of a label for later referral, including the special form
block
which prescribes a naming, and the
loop
macro that can be identified by a
named
clause. Immediate departure from a named form is possible by using the
return-from
special operator.
(block myblock
(loop for iteration from 0 do
(if (>= iteration 10)
(return-from myblock 'done)
(print iteration))))
(loop
named myloop
for iteration from 0
do (if (>= iteration 10)
(return-from myloop 'done)
(print iteration)))
In a fashion similar to C, the macros
case
,
ccase
,
ecase
,
typecase
,
ctypecase
and
etypecase
define switch statements.
(let ((my-value 5))
(case my-value
(1 (print "one"))
(2 (print "two"))
((3 4 5) (print "three four or five"))
(otherwise (print "any other value"))))
(let ((my-value 5))
(typecase my-value
(list (print "a list"))
(string (print "a string"))
(number (print "a number"))
(otherwise (print "any other type"))))
See also
*
Goto
GoTo (goto, GOTO, GO TO or other case combinations, depending on the programming language) is a statement found in many computer programming languages. It performs a one-way transfer of control to another line of code; in contrast a function c ...
*
Line number
In computing, a line number is a method used to specify a particular sequence of characters in a text file. The most common method of assigning numbers to lines is to assign every line a unique number, starting at 1 for the first line, and increm ...
*
Switch statement
In computer programming languages, a switch statement is a type of selection control mechanism used to allow the value of a variable or expression to change the control flow of program execution via search and map.
Switch statements function som ...
References
{{reflist
Source code
Control flow
Programming language concepts