Label (computer science)In programming languages, a label is a sequence of characters that identifies a location within source code. In most languages, labels take the form of an identifier, 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 CIn 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 labelsFunction 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)
{
if (number < 0)
goto error;
bar(number);
return;
error:
fprintf(stderr, "Invalid number!\n");
}
Here error is the label. The statement goto can be used to jump to a labeled statement in the code. After a Switch labelsTwo types of labels can be put in a switch statement. A case label consists of the keyword 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;
}
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. ExamplesJavascriptIn JavaScript language syntax statements may be preceded by the label: 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
It also possible to use top: {
console.log("foo")
console.log("bar")
break top
console.log("baz")
}
// Which would output:
// > foo
// > bar
Common LispIn Common Lisp two ways of defining labels exist. The first one involves the (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 (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 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 (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 alsoReferences
|
Portal di Ensiklopedia Dunia