In computer programming, a statement is a syntactic unit of an imperative programming language that expresses some action to be carried out. 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.
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, C#, C++, PHP, Java:
variable = expression;
- Fortran:
- call
- Fortran:
CALL subroutine name(parameters)
- C, C++, Java, PHP, Pascal, Ada:
subroutine name(parameters);
- Fortran:
- assertion
- C, C++, PHP:
assert(relational expression);
- Java:
assert relational expression;
- C, C++, PHP:
- goto
- Fortran:
GOTO numbered-label
- Algol 60:
goto label;
- C, C++, PHP, Pascal:
goto label;
- Fortran:
- return
- Fortran:
RETURN value
- C, C++, Java, PHP:
return value;
- Fortran:
- stop/halt/exit
- Fortran:
STOP number
- C, C++:
exit(expression)
- PHP:
exit number;
- Fortran:
Compound statements
Compound statements may contain (sequences of) statements, 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:
DO index = 1,limit <sequence> END DO
- Algol 60:
- 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:
DO WHILE (test) <sequence> END DO
- Algol 60:
- 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:
if (test) <statement> ;
- Ada:
if test then <sequence> end if;
- Fortran 77+:
IF (test) THEN <sequence> END IF
- Algol 60:
- if-statement two-way choice:
- Algol 60:
if test then <unconditional statement> else <statement> ;
- Pascal:
if test then <statement> else <statement> ;
- C, Java:
if (test) <statement> else <statement> ;
- Ada:
if test then <sequence> else <sequence> end if;
- Fortran 77+:
IF (test) THEN <sequence> ELSE <sequence> END IF
- Algol 60:
- 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.
- Up until Fortran 77, the language was described in English prose with examples, From Fortran 90 onwards, the language was described using a variant of BNF.
- Cobol used a two-dimensional metalanguage.
- Pascal used both syntax diagrams and equivalent BNF.
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
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. 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.
Expressions
A distinction is often made between statements, which are executed, and expressions, which are evaluated. Expressions always evaluate to a value, which statements do not. However, expressions are often used as part of a larger statement.
In most programming languages, a statement can consist of little more than an expression, usually by following the expression with a statement terminator (semicolon). In such a case, while the expression evaluates to a value, the complete statement does not (the expression's value is discarded). For instance, in C, C++, C#, and many similar languages, x = y + 1
is an expression that will set x to the value of y plus one, and the whole expression itself will evaluate to the same value that x is set to. However, x = y + 1;
(note the semicolon at the end) is a statement that will still set x to the value of y plus one because the expression within the statement is still evaluated, but the result of the expression is discarded, and the statement itself does not evaluate to any value.
Expressions can also be contained within other expressions. For instance, the expression x = y + 1
contains the expression y + 1
, which in turn contains the values y
and 1
, which are also technically expressions.
Although the previous examples show assignment expressions, some languages do not implement assignment as an expression, but rather as a statement. A notable example of this is Python, where = is not an operator, but rather just a separator in the assignment statement. Although Python allows multiple assignments as each assignment were an expression, this is simply a special case of the assignment statement built into the language grammar rather than a true expression.
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
- Comparison of programming languages (syntax) § Statements
- Control flow
References
- "statement". webopedia. September 1996. Retrieved 2015-03-03.
- Backus, J.W.; Bauer, F.L.; Green, J.; Katz, C.; McCarthy, J.; Naur, P.; Perlis, A.J.; Rutishauser, H.; Samuelson, K.; Vauquois, B.; Wegstein, J.H.; van Wijngaarden, A.; Woodger, M. Naur, Peter (ed.). "Revised Report on the Algorithmic Language Algol 60". mass:werk. Section "4.1". Retrieved January 23, 2021.
- Backus, J.W.; Bauer, F.L.; Green, J.; Katz, C.; McCarthy, J.; Naur, P.; Perlis, A.J.; Rutishauser, H.; Samuelson, K.; Vauquois, B.; Wegstein, J.H.; van Wijngaarden, A.; Woodger, M. Naur, Peter (ed.). "Revised Report on the Algorithmic Language Algol 60". mass:werk. Section "1.1". Retrieved January 23, 2021.
- "FORTRAN" (PDF). United States of America Standards Institute. 1966. Retrieved February 19, 2021 – via WG5 Fortran Standards.
- "Working draft J3/04-007" (PDF). J3 Fortran. May 10, 2004. Retrieved February 19, 2021.
- "ASCII COBOL Programming Reference Manual" (PDF). unisys. June 2010. Retrieved January 23, 2021.
- Jensen, Kathleen; Wirth, Niklaus (1974). Goos, G.; Hartmanis, J. (eds.). "PASCAL User Manual and Report" (PDF). Lecture Notes in Computer Science. Appendix D. Retrieved February 19, 2021.
- Knuth, D. E. (Jul 1967). "The Remaining Trouble Spots in Algol 60" (PDF). The ALGOL Family. Retrieved February 24, 2021.
- "ISO/IEC 9899:1999 (E)" (PDF). ISO/IEC. Archived (PDF) from the original on Feb 7, 2024.
- "7. Simple statements". Python 3.10.8 documentation.
External links
- PC ENCYCLOPEDIA: Definition of: program statement
In computer programming a statement is a syntactic unit of an imperative programming language that expresses some action to be carried out 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 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 statementsSimple 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 i variable i i expression i Pascal Algol 60 Ada i variable i i expression i C C C PHP Java i variable i i expression i call Fortran CALL i subroutine name i i parameters i C C Java PHP Pascal Ada i subroutine name i i parameters i assertion C C PHP assert i relational expression i Java assert i relational expression i goto Fortran GOTO numbered label Algol 60 b goto b i label i C C PHP Pascal goto i label i return Fortran RETURN i value i C C Java PHP return i value i stop halt exit Fortran STOP i number i C C exit i expression i PHP exit i number i Compound statementsCompound statements may contain sequences of statements 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 lt statement gt is any single statement could be simple or compound lt sequence gt is any sequence of zero or more lt statements gt Some programming languages provide a general way of grouping statements together so that any single lt statement gt can be replaced by a group dd Algol 60 b begin b lt sequence gt b end b Pascal begin lt sequence gt end C PHP Java lt sequence gt dd 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 span class kr if span span class n test span span class kr then span span class o lt span span class n sequence span span class o gt span span class kr end span span class kr if span span class p span dd 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 b for b index 1 b step b 1 b until b limit b do b lt statement gt Pascal span class k for span span class w span span class kp index span span class w span span class o span span class w span span class mi 1 span span class w span span class k to span span class w span span class n limit span span class w span span class k do span span class w span span class o lt span span class n statement span span class o gt span span class w span span class o span C Java span class k for span span class w span span class p span span class w span span class n index span span class w span span class o span span class w span span class mi 1 span span class p span span class w span span class n index span span class w span span class o lt span span class w span span class n limit span span class p span span class w span span class n index span span class w span span class o span span class w span span class mi 1 span span class p span span class w span span class o lt span span class n statement span span class o gt span span class w span span class p span Ada span class kr for span span class n index span span class ow in span span class mf 1 span span class p span span class n limit span span class kr loop span span class o lt span span class n sequence span span class o gt span span class kr end span span class kr loop span Fortran 90 DO index 1 limit lt sequence gt END DO condition controlled loop with test at start of loop Algol 60 b for b index expression b while b test b do b lt statement gt Pascal span class k while span span class w span span class n test span span class w span span class k do span span class w span span class o lt span span class n statement span span class o gt span span class w span span class o span C Java span class k while span span class w span span class p span span class n test span span class p span span class w span span class o lt span span class n statement span span class o gt span span class w span span class p span Ada span class kr while span span class n test span span class kr loop span span class o lt span span class n sequence span span class o gt span span class kr end span span class kr loop span Fortran 90 DO WHILE test lt sequence gt END DO condition controlled loop with test at end of loop Pascal span class k repeat span span class w span span class o lt span span class n sequence span span class o gt span span class w span span class k until span span class w span span class n test span span class o span span class w span span class cm note reversed test span C Java span class k do span span class w span span class p span span class w span span class o lt span span class n sequence span span class o gt span span class w span span class p span span class w span span class k while span span class w span span class p span span class n test span span class p span span class w span span class p span Ada span class kr loop span span class o lt span span class n sequence span span class o gt span span class kr exit span span class kr when span span class n test span span class p span span class kr end span span class kr loop span span class p span condition controlled loop with test in the middle of the loop C span class k do span span class w span span class p span span class w span span class o lt span span class n sequence span span class o gt span span class w span span class k if span span class w span span class p span span class n test span span class p span span class w span span class k break span span class p span span class w span span class o lt span span class n sequence span span class o gt span span class w span span class p span span class w span span class k while span span class w span span class p span span class nb true span span class p span span class w span span class p span Ada span class kr loop span span class o lt span span class n sequence span span class o gt span span class kr exit span span class kr when span span class n test span span class p span span class o lt span span class n sequence span span class o gt span span class kr end span span class kr loop span span class p span if statement simple situation Algol 60 b if b test b then b lt unconditional statement gt Pascal span class k if span span class w span span class n test span span class w span span class k then span span class w span span class o lt span span class n statement span span class o gt span span class w span span class o span C Java span class k if span span class w span span class p span span class n test span span class p span span class w span span class o lt span span class n statement span span class o gt span span class w span span class p span Ada span class kr if span span class n test span span class kr then span span class o lt span span class n sequence span span class o gt span span class kr end span span class kr if span span class p span Fortran 77 IF test THEN lt sequence gt END IF if statement two way choice Algol 60 b if b test b then b lt unconditional statement gt b else b lt statement gt Pascal span class k if span span class w span span class n test span span class w span span class k then span span class w span span class o lt span span class n statement span span class o gt span span class w span span class k else span span class w span span class o lt span span class n statement span span class o gt span span class w span span class o span C Java span class k if span span class w span span class p span span class n test span span class p span span class w span span class o lt span span class n statement span span class o gt span span class w span span class k else span span class w span span class o lt span span class n statement span span class o gt span span class w span span class p span Ada span class kr if span span class n test span span class kr then span span class o lt span span class n sequence span span class o gt span span class kr else span span class o lt span span class n sequence span span class o gt span span class kr end span span class kr if span span class p span Fortran 77 IF test THEN lt sequence gt ELSE lt sequence gt END IF case switch statement multi way choice Pascal span class k case span span class w span span class n c span span class w span span class k of span span class w span span class s a span span class o span span class w span span class n alert span span class p span span class o span span class w span span class s q span span class o span span class w span span class n quit span span class p span span class o span span class w span span class k end span span class o span Ada span class kr case span span class n c span span class kr is span span class kr when span span class p span span class na a span span class p span span class p gt span span class n alert span span class p span span class kr when span span class p span span class na q span span class p span span class p gt span span class n quit span span class p span span class kr end span span class kr case span span class p span C Java span class k switch span span class w span span class p span span class n c span span class p span span class w span span class p span span class w span span class k case span span class w span span class sc a span span class p span span class w span span class n alert span span class p span span class w span span class k break span span class p span span class w span span class k case span span class w span span class sc q span span class p span span class w span span class n quit span span class p span span class w span span class k break span span class p span span class w span span class p span Exception handling Ada begin i protected code i except when i exception specification i gt i exception handler i Java try i protected code i catch i exception specification i i exception handler i finally i cleanup i Python try i protected code i except i exception specification i i exception handler i else i no exceptions i finally i cleanup i SyntaxApart 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 Up until Fortran 77 the language was described in English prose with examples From Fortran 90 onwards the language was described using a variant of BNF Cobol used a two dimensional metalanguage Pascal used both syntax diagrams and equivalent BNF 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 second IF and the first THEN are variables in Fortran IF A X 10 conditional statement with other variants IF A 2 assignment to a subscripted variable named IFAs 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 5 DO 10 I 1 5 assignment of the value 1 5 to the variable DO10I dd Flagged words In Algol 60 and Algol 68 special tokens were distinguished explicitly for publication in boldface e g b begin b for programming with some special marking e g a flag begin quotation marks begin or underlined u begin u 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 SemanticsSemantics 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 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 ExpressionsA distinction is often made between statements which are executed and expressions which are evaluated Expressions always evaluate to a value which statements do not However expressions are often used as part of a larger statement In most programming languages a statement can consist of little more than an expression usually by following the expression with a statement terminator semicolon In such a case while the expression evaluates to a value the complete statement does not the expression s value is discarded For instance in C C C and many similar languages x y 1 is an expression that will set x to the value of y plus one and the whole expression itself will evaluate to the same value that x is set to However x y 1 note the semicolon at the end is a statement that will still set x to the value of y plus one because the expression within the statement is still evaluated but the result of the expression is discarded and the statement itself does not evaluate to any value Expressions can also be contained within other expressions For instance the expression x y 1 contains the expression y 1 which in turn contains the values y and 1 which are also technically expressions Although the previous examples show assignment expressions some languages do not implement assignment as an expression but rather as a statement A notable example of this is Python where is not an operator but rather just a separator in the assignment statement Although Python allows multiple assignments as each assignment were an expression this is simply a special case of the assignment statement built into the language grammar rather than a true expression ExtensibilityMost 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 alsoComparison of programming languages syntax Statements Control flowReferences statement webopedia September 1996 Retrieved 2015 03 03 Backus J W Bauer F L Green J Katz C McCarthy J Naur P Perlis A J Rutishauser H Samuelson K Vauquois B Wegstein J H van Wijngaarden A Woodger M Naur Peter ed Revised Report on the Algorithmic Language Algol 60 mass werk Section 4 1 Retrieved January 23 2021 Backus J W Bauer F L Green J Katz C McCarthy J Naur P Perlis A J Rutishauser H Samuelson K Vauquois B Wegstein J H van Wijngaarden A Woodger M Naur Peter ed Revised Report on the Algorithmic Language Algol 60 mass werk Section 1 1 Retrieved January 23 2021 FORTRAN PDF United States of America Standards Institute 1966 Retrieved February 19 2021 via WG5 Fortran Standards Working draft J3 04 007 PDF J3 Fortran May 10 2004 Retrieved February 19 2021 ASCII COBOL Programming Reference Manual PDF unisys June 2010 Retrieved January 23 2021 Jensen Kathleen Wirth Niklaus 1974 Goos G Hartmanis J eds PASCAL User Manual and Report PDF Lecture Notes in Computer Science Appendix D Retrieved February 19 2021 Knuth D E Jul 1967 The Remaining Trouble Spots in Algol 60 PDF The ALGOL Family Retrieved February 24 2021 ISO IEC 9899 1999 E PDF ISO IEC Archived PDF from the original on Feb 7 2024 7 Simple statements Python 3 10 8 documentation External linksPC ENCYCLOPEDIA Definition of program statement