In
computer programming
Computer programming or coding is the composition of sequences of instructions, called computer program, programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of proc ...
, a statement is a
syntactic
In linguistics, syntax ( ) is the study of how words and morphemes combine to form larger units such as phrases and sentences. Central concerns of syntax include word order, grammatical relations, hierarchical sentence structure (constituency ...
unit of an
imperative programming language
In computer science, imperative programming is a programming paradigm of software that uses statements that change a program's state. In much the same way that the imperative mood in natural languages expresses commands, an imperative program con ...
that expresses some action to be carried out. A
program
Program (American English; also Commonwealth English in terms of computer programming and related activities) or programme (Commonwealth English in all other meanings), programmer, or programming may refer to:
Business and management
* Program m ...
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
Ada may refer to:
Arts and entertainment
* '' Ada or Ardor: A Family Chronicle'', a novel by Vladimir Nabokov
Film and television
* Ada, a character in 1991 movie '' Armour of God II: Operation Condor''
* '' Ada... A Way of Life'', a 2008 Bollywo ...
,
Algol 60
ALGOL 60 (short for ''Algorithmic Language 1960'') is a member of the ALGOL family of computer programming languages. It followed on from ALGOL 58 which had introduced code blocks and the begin and end pairs for delimiting them, representing a ...
,
C,
Java
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
,
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
In linguistics, syntax ( ) is the study of how words and morphemes combine to form larger units such as phrases and sentences. Central concerns of syntax include word order, grammatical relations, hierarchical sentence structure (constituenc ...
or grammar. The meaning of a statement is determined by its
semantics
Semantics is the study of linguistic Meaning (philosophy), meaning. It examines what meaning is, how words get their meaning, and how the meaning of a complex expression depends on its parts. Part of this process involves the distinction betwee ...
.
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
Return may refer to:
In business, economics, and finance
* Return on investment (ROI), the financial gain after an expense.
* Rate of return, the financial term for the profit or loss derived from an investment
* Tax return, a blank document or t ...
, 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'';
*
call
Call or Calls may refer to:
Arts, entertainment, and media Games
* Call (poker), a bet matching an opponent's
* Call, in the game of contract bridge, a bid, pass, double, or redouble in the bidding stage
Music and dance
* Call (band), from L ...
**Fortran:
CALL ''subroutine name''(''parameters'')
**C, C++, Java, PHP, Pascal, Ada:
''subroutine name''(''parameters'');
*
assertion
**C, C++, PHP:
assert(''relational expression'');
**Java:
assert ''relational expression'';
*
goto
**Fortran:
GOTO numbered-label
**Algol 60:
goto ''label'';
**C, C++, PHP, Pascal:
goto ''label'';
*
return
Return may refer to:
In business, economics, and finance
* Return on investment (ROI), the financial gain after an expense.
* Rate of return, the financial term for the profit or loss derived from an investment
* Tax return, a blank document or t ...
**Fortran:
RETURN ''value''
**C, C++, Java, PHP:
return ''value'';
*
stop/halt/exit
**Fortran:
STOP ''number''
**C, C++:
exit(''expression'')
**PHP:
exit ''number'';
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:
::* is any single statement (could be simple or compound).
::* is any sequence of zero or more
::Some programming languages provide a general way of grouping statements together, so that any single can be replaced by a group:
::* Algol 60:
begin end
::* Pascal:
begin end
::* C, PHP, Java:
::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:
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 ;
** Pascal:
** C, Java:
** Ada:
** Fortran 90:
*
condition-controlled loop with test at start of loop:
** Algol 60:
for index := expression while test do ;
** Pascal:
** C, Java:
** Ada:
** Fortran 90:
*
condition-controlled loop with test at end of loop:
** Pascal:
** C, Java:
** Ada:
* condition-controlled loop with test in the middle of the loop:
** C:
** Ada:
*
if-statement simple situation:
** Algol 60:
if test then ;
** Pascal:
** C, Java:
** Ada:
** Fortran 77+:
*
if-statement two-way choice:
** Algol 60:
if test then else ;
** Pascal:
** C, Java:
** Ada:
** Fortran 77+:
*
case/switch statement multi-way choice:
** Pascal:
** Ada:
** C, Java:
*
Exception handling
In computing and computer programming, exception handling is the process of responding to the occurrence of ''exceptions'' – anomalous or exceptional conditions requiring special processing – during the execution of a program. In general, an ...
:
** Ada:
begin ''protected code'' except when ''exception specification'' => ''exception handler''
** Java:
try catch (''exception specification'') finally
** Python:
try: ''protected code'' except ''exception specification'': ''exception handler'' else: ''no exceptions'' finally: ''cleanup''
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
In computer science, Backus–Naur form (BNF, pronounced ), also known as Backus normal form, is a notation system for defining the Syntax (programming languages), syntax of Programming language, programming languages and other Formal language, for ...
(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 diagram
Syntax diagrams (or railroad diagrams) are a way to represent a context-free grammar. They represent a graphical alternative to Backus–Naur form, EBNF, Augmented Backus–Naur form, and other text-based grammars as metalanguages. Early books ...
s and equivalent BNF.
BNF uses recursion to express repetition, so various
extensions
Extension, extend or extended may refer to:
Mathematics
Logic or set theory
* Axiom of extensionality
* Extensible cardinal
* Extension (model theory)
* Extension (proof theory)
* Extension (predicate logic), the set of tuples of values t ...
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
An identifier is a name that identifies (that is, labels the identity of) either a unique object or a unique ''class'' of objects, where the "object" or class may be an idea, person, physical countable object (or class thereof), or physical mass ...
. This often leads to
grammars
In linguistics, grammar is the set of rules for how a natural language is structured, as demonstrated by its speakers or writers. Grammar rules may concern the use of clauses, phrases, and words. The term may also refer to the study of such rul ...
which are easier to
parse
Parsing, syntax analysis, or syntactic analysis is a process of analyzing a string of symbols, either in natural language, computer languages or data structures, conforming to the rules of a formal grammar by breaking it into parts. The term ''pa ...
, 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
IF
::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 5
:*
DO 10 I = 1.5
assignment of the value 1.5 to the variable
DO10I
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
The Elliott 503 was a transistorized computer introduced by Elliott Brothers in 1963. It was software-compatible with the earlier Elliott 803 but was about 70 times faster and a more powerful machine. About 32 units were sold. The basic config ...
). 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
FLOW-MATIC, originally known as B-0 (Business Language version 0), was the first English-like data processing language. It was developed for the UNIVAC I at Remington Rand
Remington Rand, Inc. was an early American business machine manufactu ...
(1953) and
COBOL
COBOL (; an acronym for "common business-oriented language") is a compiled English-like computer programming language designed for business use. It is an imperative, procedural, and, since 2002, object-oriented language. COBOL is primarily ...
(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
A label (as distinct from signage) is a piece of paper, plastic film, cloth, metal, or other material affixed to a container or product. Labels are most often affixed to packaging and containers using an adhesive, or sewing when affixed to ...
, using
if
and
goto
.
The
semantics
Semantics is the study of linguistic Meaning (philosophy), meaning. It examines what meaning is, how words get their meaning, and how the meaning of a complex expression depends on its parts. Part of this process involves the distinction betwee ...
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
Python may refer to:
Snakes
* Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia
** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia
* Python (mythology), a mythical serpent
Computing
* Python (prog ...
, 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
*
*
Control flow
In computer science, control flow (or flow of control) is the order in which individual statements, instructions or function calls of an imperative program are executed or evaluated. The emphasis on explicit control flow distinguishes an '' ...
References
External links
PC ENCYCLOPEDIA: Definition of: program statement
{{DEFAULTSORT:Statement (Programming)
Programming language concepts
Statements