Contents
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 statement. In assembly language, labels can be used anywhere an address can (for example, as the operand of a or instruction). Also in Pascal and its derived variations. Some languages, such as Fortran and BASIC, support numeric labels. Labels are also used to identify an entry point into a compiled sequence of statements (e.g., during debugging).
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. Here error is the label. The statement goto can be used to jump to a labeled statement in the code. After a, 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, followed by an expression that evaluates to integer constant. A default label consists of the keyword. 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. 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 syntax statements may be preceded by the label: It also possible to use statement to break out of the code blocks:
Common Lisp
In Common Lisp two ways of defining labels exist. The first one involves the 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 labels are defined as forms starting with a symbol; the special form permits a transfer of control between these labels. A second method utilizes the reader macros and , 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 declaring and initializing a “variable” and accessing it. The placeholder n designates a chosen unsigned decimal integer identifying the label. Apart from that, some forms permit or mandate the declaration of a label for later referral, including the special form which prescribes a naming, and the macro that can be identified by a clause. Immediate departure from a named form is possible by using the special operator. In a fashion similar to C, the macros, , , , and define switch statements.
This article is derived from Wikipedia and licensed under CC BY-SA 4.0. View the original article.
Wikipedia® is a registered trademark of the
Wikimedia Foundation, Inc.
Bliptext is not
affiliated with or endorsed by Wikipedia or the
Wikimedia Foundation.