In computer programming, a statement is a syntactic unit of an imperative programming language that expresses some action to be carried out.[1] A program written in such a language is formed by a sequence of one or more statements. A statement may have internal components (e.g., expressions).
Many programming languages (e.g. Ada, Algol 60, C, Java, Pascal) make a distinction between statements and definitions/declarations. A definition or declaration specifies the data on which a program is to operate, while a statement specifies the actions to be taken with that data.
Statements which cannot contain other statements are simple; those which can contain other statements are compound.[2]
The appearance of a statement (and indeed a program) is determined by its syntax or grammar. The meaning of a statement is determined by its semantics.
Simple statements are complete in themselves; these include assignments, subroutine calls, and a few statements which may significantly affect the program flow of control (e.g. goto, return, stop/halt). In some languages, input and output, assertions, and exits are handled by special statements, while other languages use calls to predefined subroutines.
variable = expression
variable := expression;
variable = expression;
CALL subroutine name(parameters)
subroutine name(parameters);
assert(relational expression);
assert relational expression;
GOTO numbered-label
goto label;
goto label;
RETURN value
return value;
STOP number
exit(expression)
exit number;
Compound statements may contain (sequences of) statements, are nestable to any reasonable depth, and generally involve tests to decide whether or not to obey or repeat these contained statements.
begin <sequence> end
begin <sequence> end
{ <sequence> }
if test then <sequence> end if;
Many compound statements are loop commands or choice commands. In theory only one of each of these types of commands is required. In practice there are various special cases which occur quite often; these may make a program easier to understand, may make programming easier, and can often be implemented much more efficiently. There are many subtleties not mentioned here; see the linked articles for details.
for index := 1 step 1 until limit do <statement> ;
for index := 1 to limit do <statement> ;
for ( index = 1; index <= limit; index += 1) <statement> ;
for index in 1..limit loop <sequence> end loop
for index := expression while test do <statement> ;
while test do <statement> ;
while (test) <statement> ;
while test loop <sequence> end loop
repeat <sequence> until test; { note reversed test}
do { <sequence> } while (test) ;
loop <sequence> exit when test; end loop;
do { <sequence> if (test) break; <sequence> } while (true) ;
loop <sequence> exit when test; <sequence> end loop;
if test then <unconditional statement> ;
if test then <statement> ;
if (test) <statement> ;
if test then <sequence> end if;
if test then <unconditional statement> else <statement> ;
if test then <statement> else <statement> ;
it (test) <statement> else <statement> ;
if test then <sequence> else <sequence> end if;
case c of 'a': alert(); 'q': quit(); end;
case c is when 'a' => alert(); when 'q' => quit(); end case;
switch (c) { case 'a': alert(); break; case 'q': quit(); break; }
begin protected code except when exception specification => exception handler
try { protected code } catch (exception specification) { exception handler } finally { cleanup }
try: protected code except exception specification: exception handler else: no exceptions finally: cleanup
Apart from assignments and subroutine calls, most languages start each statement with a special word (e.g. goto, if, while, etc.) as shown in the above examples. Various methods have been used to describe the form of statements in different languages; the more formal methods tend to be more precise:
BNF uses recursion to express repetition, so various extensions have been proposed to allow direct indication of repetition.
Some programming language grammars reserve keywords or mark them specially, and do not allow them to be used as identifiers. This often leads to grammars which are easier to parse, requiring less lookahead.
Fortran and PL/1 do not have reserved keywords, allowing statements like:
IF IF = THEN THEN ...
(the second IF
and the first THEN
are variables).IF (A) X = 10...
conditional statement (with other variants)IF (A) = 2
assignment to a subscripted variable named IF
DO 10 I = 1,5
start of a loop with I running from 1 to 5DO 10 I = 1.5
assignment of the value 1.5 to the variable DO10I
In Algol 60 and Algol 68, special tokens were distinguished explicitly: for publication, in boldface e.g. begin
; for programming, with some special marking, e.g., a flag ('begin
), quotation marks ('begin'
), or underlined (begin
on the Elliott 503). This is called "stropping".
Tokens that are part of the language syntax thus do not conflict with programmer-defined names.
Certain names are reserved as part of the programming language and can not be used as programmer-defined names. The majority of the most popular programming languages use reserved keywords. Early examples include FLOW-MATIC (1953) and COBOL (1959). Since 1970 other examples include Ada, C, C++, Java, and Pascal. The number of reserved words depends on the language: C has about 30 while COBOL has about 400.
Semantics is concerned with the meaning of a program. The standards documents for many programming languages use BNF or some equivalent to express the syntax/grammar in a fairly formal and precise way, but the semantics/meaning of the program is generally described using examples and English prose. This can result in ambiguity.[8] In some language descriptions the meaning of compound statements is defined by the use of 'simpler' constructions, e.g. a while loop can be defined by a combination of tests, jumps, and labels, using if
and goto
.
The semantics article describes several mathematical/logical formalisms which have been used to specify semantics in a precise way; these are generally more complicated than BNF, and no single approach is generally accepted as the way to go. Some approaches effectively define an interpreter for the language, some use formal logic to reason about a program, some attach affixes to syntactic entities to ensure consistency, etc.
A distinction is often made between statements, which are executed, and expressions, which are evaluated. The value obtained from an expression is often used as part of a statement e.g. assignment variable := expression;
Some programming languages (e.g. C, C++) allow some statements to provide a result (technically all statements provide a result, but this result is often of type 'void' and can't be used for anything). The most useful statement which provides a result is an assignment, the result being the value just assigned.
This can be useful for multiple initialisation:
i = j = 0;
which is treated as i = (j = 0);
It can also result in simple finger trouble completely changing the meaning of some code:
if (i == j) {. . . };
tests to see if i
is equal to j
if (i = j) { . . . };
assigns the value of j
to i
and then tests to see if that value is non-zero.Some languages (Algol 60, Pascal) allow multiple assignment but don't allow assignments to appear in expressions.
Most languages have a fixed set of statements defined by the language, but there have been experiments with extensible languages that allow the programmer to define new statements.
By: Wikipedia.org
Edited: 2021-06-18 12:26:17
Source: Wikipedia.org