{{short description|Sequence of characters that identifies a location within source code}}

In [[programming language]]s, a '''label''' is a sequence of characters that identifies a location within [[source code]]. In most languages, labels take the form of an [[Identifier (computer languages)|identifier]], often followed by a [[punctuation|punctuation character]] (e.g., a [[Colon (punctuation)|colon]]). In many [[high level programming languages|high-level languages]], the purpose of a label is to act as the destination of a <code>[[GOTO]]</code> statement.<ref>{{Cite web |url=http://c0x.coding-guidelines.com/6.8.6.1.html |title=C Standard section 6.8.6.1 The goto statement |access-date=2008-07-03 |archive-date=2007-12-24 |archive-url=https://web.archive.org/web/20071224215314/http://c0x.coding-guidelines.com/6.8.6.1.html |url-status=dead }}</ref><ref>{{cite web|url=http://www.qbasicnews.com/qboho/qckgoto.shtml|title=GOTO Statement QuickSCREEN|date=1988|accessdate=2008-07-03|publisher=Microsoft|archive-date=2009-07-25|archive-url=https://web.archive.org/web/20090725050926/http://www.qbasicnews.com/qboho/qckgoto.shtml|url-status=dead}}</ref> In [[assembly language]], labels can be used anywhere an [[Memory address|address]] can (for example, as the operand of a <code>[[JMP (x86 instruction)|JMP]]</code> or <code>[[MOV (x86 instruction)|MOV]]</code> instruction).<ref>{{cite web|url=http://www.cs.uaf.edu/2007/fall/cs301/support/x86/index.html|title=nasm x86 Assembly|date=|accessdate=2008-07-03|author=O. Lawlor}}</ref> Also in [[Pascal (programming language)|Pascal]] and its derived variations. Some languages, such as [[Fortran]] and [[BASIC]], support numeric labels.<ref name="GBvsQB">{{cite web|url=http://support.microsoft.com:80/kb/73084|title=Differences Between GW-BASIC and QBasic|archive-url=https://web.archive.org/web/20100210111932/http://support.microsoft.com:80/kb/73084|archive-date=2010-02-10}}</ref> Labels are also used to identify an entry point into a [[compiler|compiled]] sequence of statements (e.g., during [[debugging]]).

== C == In [[C (programming language)|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 === {{See also|goto}}

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 [[variable (computer science)|variables]] and [[subroutine|functions]] with the same name as a label.

<syntaxhighlight lang="c"> void foo(int number) { if (number < 0) goto error; bar(number); return; error: fprintf(stderr, "Invalid number!\n"); } </syntaxhighlight>

Here ''error'' is the label. The statement [[goto]] can be used to jump to a labeled statement in the code. After a <code>goto</code>, program execution continues with the statement after the label.

=== Switch labels === {{See also|Switch statement}}

Two types of labels can be put in a switch statement. A case label consists of the keyword <code>case</code>, followed by an expression that evaluates to integer constant. A default label consists of the keyword <code>default</code>. 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.

<syntaxhighlight lang="c"> switch (die) { default: printf("invalid\n"); break;

case 1: case 3: case 5: printf("odd\n"); break;

case 2: case 4: case 6: printf("even\n"); break; } </syntaxhighlight>

Within a single [[switch statement]], 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 [[JavaScript syntax|syntax]] statements may be preceded by the label: <syntaxhighlight lang="javascript"> top: //Label the outermost for-loop. for (var i = 0; i < 4; i++) { for (var j = 0; j < 4; j++) { if (j === 3 && i === 2) { alert("i=" + i + ", j=" + j); //i=2, j=3 break top; } } }

alert("i=" + i + ", j=" + j); //i=2, j=3 </syntaxhighlight>

It also possible to use <syntaxhighlight inline lang="javascript">break</syntaxhighlight> statement to break out of the code blocks:

<syntaxhighlight lang="javascript"> top: { console.log("foo") console.log("bar") break top console.log("baz")

} // Which would output: // > foo // > bar

</syntaxhighlight>

== Common Lisp ==

In [[Common Lisp]] two ways of defining labels exist. The first one involves the <code>tagbody</code> special operator. Distinguishing its usage from many other programming languages that permit global navigation, such as [[C (programming language)|C]], the labels are only accessible in the context of this operator. Inside of a <code>tagbody</code> labels are defined as forms starting with a symbol; the <code>go</code> special form permits a transfer of control between these labels.<ref>{{cite web|url=http://www.lispworks.com/documentation/HyperSpec/Body/s_tagbod.htm|title=CLHS: Special Operator TAGBODY|date=|accessdate=2020-08-18|author=Kent Pitman}}</ref>

<syntaxhighlight lang="lisp"> (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))) </syntaxhighlight>

A second method utilizes the reader macros <code>#''n''=</code> and <code>#''n''#</code>, the former of which labels the object immediately following it, the latter refers to its evaluated value.<ref>{{cite web|url=http://www.lispworks.com/documentation/HyperSpec/Body/02_dh.htm|title=CLHS: Section 2.4.8|date=|accessdate=2020-08-18|author=Kent Pitman}}</ref> Labels in this sense constitute rather an alternative to variables, with <code>#''n''=</code> declaring and initializing a “variable” and <code>#''n''#</code> accessing it. The placeholder ''n'' designates a chosen unsigned decimal integer identifying the label.

<syntaxhighlight lang="lisp"> (progn #1="hello" (print #1#)) </syntaxhighlight>

Apart from that, some forms permit or mandate the declaration of a label for later referral, including the special form <code>block</code> which prescribes a naming, and the <code>loop</code> macro that can be identified by a <code>named</code> clause. Immediate departure from a named form is possible by using the <code>return-from</code> special operator.

<syntaxhighlight lang="lisp"> (block myblock (loop for iteration from 0 do (if (>= iteration 10) (return-from myblock 'done) (print iteration)))) </syntaxhighlight>

<syntaxhighlight lang="lisp"> (loop named myloop for iteration from 0 do (if (>= iteration 10) (return-from myloop 'done) (print iteration))) </syntaxhighlight>

In a fashion similar to C, the macros <code>case</code>, <code>ccase</code>, <code>ecase</code>,<ref>{{cite web|url=http://www.lispworks.com/documentation/HyperSpec/Body/m_case_.htm|title=CLHS: Macro CASE, CCASE, ECASE|date=|accessdate=2020-08-20|author=Kent Pitman}}</ref> <code>typecase</code>, <code>ctypecase</code> and <code>etypecase</code> define switch statements.<ref>{{cite web|url=http://www.lispworks.com/documentation/HyperSpec/Body/m_tpcase.htm|title=CLSH: Macro TYPECASE, CTYPECASE, ETYPECASE|date=|accessdate=2020-08-20|author=Kent Pitman}}</ref>

<syntaxhighlight lang="lisp"> (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")))) </syntaxhighlight>

<syntaxhighlight lang="lisp"> (let ((my-value 5)) (typecase my-value (list (print "a list")) (string (print "a string")) (number (print "a number")) (otherwise (print "any other type")))) </syntaxhighlight>

==See also== * [[Line number]]

==References== {{reflist}}

[[Category:Source code]] [[Category:Control flow]] [[Category:Programming language concepts]] [[Category:Articles with example C code]] [[Category:Articles with example JavaScript code]] [[Category:Articles with example Lisp (programming language) code]]