Statement (computer science)
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
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.
- assignment
- Fortran:
variable = expression
- Pascal, Algol 60, Ada:
variable := expression;
- C, PHP, Java:
variable = expression;
- Fortran:
- call
- Fortran:
CALL subroutine name(parameters)
- C, Java, PHP, Pascal, Ada:
subroutine name(parameters);
- Fortran:
- assertion
- PHP:
assert(relational expression);
- Java:
assert relational expression;
- PHP:
- goto
- Fortran:
GOTO numbered-label
- Algol 60:
goto label;
- C, PHP, Pascal:
goto label;
- Fortran:
- return
- Fortran:
RETURN value
- C, Java, PHP:
return value;
- Fortran:
- stop/halt/exit
- Fortran:
STOP number
- PHP:
exit number;
- Fortran:
Compound statements
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.
- Notation for the following examples:
- <statement> is any single statement (could be simple or compound).
- <sequence> is any sequence of zero or more <statements>
- Some programming languages provide a general way of grouping statements together, so that any single <statement> can be replaced by a group:
- Notation for the following examples:
- Algol 60:
begin <sequence> end
- Pascal:
begin <sequence> end
- C, PHP, Java:
{ <sequence> }
- Algol 60:
- Other programming languages have a different special terminator on each kind of compound statement, so that one or more statements are automatically treated as a group:
- Ada:
if test then <sequence> end if;
- Ada:
- Other programming languages have a different special terminator on each kind of compound statement, so that one or more statements are automatically treated as a group:
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.
- count-controlled loop:
- Algol 60:
for index := 1 step 1 until limit do <statement> ;
- Pascal:
for index := 1 to limit do <statement> ;
- C, Java:
for ( index = 1; index <= limit; index += 1) <statement> ;
- Ada:
for index in 1..limit loop <sequence> end loop
- Fortran 90:
- Algol 60:
- DO index = 1,limit
- <sequence>
- END DO
- DO index = 1,limit
- condition-controlled loop with test at start of loop:
- Algol 60:
for index := expression while test do <statement> ;
- Pascal:
while test do <statement> ;
- C, Java:
while (test) <statement> ;
- Ada:
while test loop <sequence> end loop
- Fortran 90:
- Algol 60:
- DO WHILE (test)
- <sequence>
- END DO
- DO WHILE (test)
- condition-controlled loop with test at end of loop:
- Pascal:
repeat <sequence> until test; { note reversed test}
- C, Java:
do { <sequence> } while (test) ;
- Ada:
loop <sequence> exit when test; end loop;
- Pascal:
- condition-controlled loop with test in the middle of the loop:
- C:
do { <sequence> if (test) break; <sequence> } while (true) ;
- Ada:
loop <sequence> exit when test; <sequence> end loop;
- C:
- if-statement simple situation:
- Algol 60:
if test then <unconditional statement> ;
- Pascal:
if test then <statement> ;
- C, Java:
it (test) <statement> ;
- Ada:
if test then <sequence> end if;
- Fortran 77+:
- Algol 60:
- IF (test) THEN
- <sequence>
- END IF
- IF (test) THEN
- if-statement two-way choice:
- Algol 60:
if test then <unconditional statement> else <statement> ;
- Pascal:
if test then <statement> else <statement> ;
- C, Java:
it (test) <statement> else <statement> ;
- Ada:
if test then <sequence> else <sequence> end if;
- Fortran 77+:
- Algol 60:
- IF (test) THEN
- <sequence>
- ELSE
- <sequence>
- END IF
- IF (test) THEN
- case/switch statement multi-way choice:
- Pascal:
case c of 'a': alert(); 'q': quit(); end;
- Ada:
case c is when 'a' => alert(); when 'q' => quit(); end case;
- C, Java:
switch (c) { case 'a': alert(); break; case 'q': quit(); break; }
- Pascal:
- Exception handling:
- Ada:
begin protected code except when exception specification => exception handler
- Java:
try { protected code } catch (exception specification) { exception handler } finally { cleanup }
- Python:
try: protected code except exception specification: exception handler else: no exceptions finally: cleanup
- Ada:
Syntax
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:
- Algol 60 used Backus–Naur form (BNF) which set a new level for language grammar specification.[3]
- Up until Fortran 77, the language was described in English prose with examples,[4] From Fortran 90 onwards, the language was described using a variant of BNF.[5]
- Cobol used a two-dimensional metalanguage.[6]
- Pascal used both syntax diagrams and equivalent BNF.[7]
BNF uses recursion to express repetition, so various extensions have been proposed to allow direct indication of repetition.
Statements and keywords
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.
No distinguished keywords
Fortran and PL/1 do not have reserved keywords, allowing statements like:
- in PL/1:
IF IF = THEN THEN ...
(the secondIF
and the firstTHEN
are variables).
- in Fortran:
IF (A) X = 10...
conditional statement (with other variants)IF (A) = 2
assignment to a subscripted variable namedIF
- As spaces were optional up to Fortran 95, a typo could completely change the meaning of a statement:
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 variableDO10I
Flagged words
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.
Reserved keywords
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
In most programming languages, most statements cannot be modeled as subroutine calls. For example, the assignment statement a = b
treats a
as an L-value, but b
as an R-value; the loop statement while X do Y
may execute X
and Y
multiple times. Some programming languages do support non-strict mechanisms which allow modeling such cases.
Expressions
In most languages, statements contrast with expressions in that statements do not return results and are executed solely for their side effects, while expressions always return a result and often do not have side effects at all.
For example:
- A statement
print('Hello, World.')
- An expression:
X=your data
print (X)
Among imperative programming languages, Algol 68 is one of the few in which a statement can return a result. In languages that mix imperative and functional styles, such as the Lisp family, the distinction between expressions and statements is not made: even expressions executed in sequential contexts solely for their side effects and whose return values are not used are considered 'expressions'. In purely functional programming, there are no statements; everything is an expression.
This distinction is frequently observed in wording: a statement is executed, while an expression is evaluated. This is found in the exec
and eval
functions found in some languages: in Python both are found, with exec
applied to statements and eval
applied to expressions.
A statement is an instruction that the Python interpreter can execute. We have only seen the assignment statement so far. Some other kinds of statements that we’ll see shortly are while statements, for statements, if statements, and import statements. (There are other kinds too!)
An expression is a combination of values, variables, operators, and calls to functions. Expressions need to be evaluated. If you ask Python to print an expression, the interpreter evaluates the expression and displays the result.
Extensibility
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.
See also
References
- ^ "statement". webopedia. Retrieved 2015-03-03.
- ^ Revised ALGOL 60 report section. 4.1."ALGOL 60". Retrieved January 23, 2021.
- ^ Revised ALGOL 60 report section 1.1."ALGOL 60". Retrieved January 23, 2021.
- ^ ANSI FORTRAN 66 standard"FORTRAN 66" (PDF). Retrieved February 19, 2021.
- ^ ANSI FORTRAN 95 standard"Fortran95" (PDF). Retrieved February 19, 2021.
- ^ Cobol manual."COBOL" (PDF). Retrieved January 23, 2021.
- ^ Pascal User Manual and Report Appendix D."Pascal" (PDF). Retrieved February 19, 2021.