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.''variable'' = ''expression''
**Pascal, Algol 60, Ada: ''variable'' := ''expression'';
**C, C#, C++, PHP, Java: ''variable'' = ''expression'';
* call
**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 numbered-label
**Algol 60: goto ''label'';
**C, C++, PHP, Pascal: goto ''label'';
* 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: ::*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: if test then 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.
* count-controlled loop:
** Algol 60: for index := 1 step 1 until limit do ;
** Pascal: for index := 1 to limit do ;
** C, Java: for ( index = 1; index <= limit; index += 1) ;
** Ada: for index in 1..limit loop end loop
** Fortran 90:
* condition-controlled loop with test at start of loop:
** Algol 60: for index := expression while test do ;
** Pascal: while test do ;
** C, Java: while (test) ;
** Ada: while test loop end loop
** Fortran 90:
* condition-controlled loop with test at end of loop:
** Pascal: repeat until test;
** C, Java: do while (test) ;
** Ada: loop exit when test; end loop;
* condition-controlled loop with test in the middle of the loop:
** C: do while (true) ;
** Ada: loop exit when test; end loop;
* if-statement simple situation:
** Algol 60:if test then ;
** Pascal:if test then ;
** C, Java: if (test) ;
** Ada: if test then end if;
** Fortran 77+:
* if-statement two-way choice:
** Algol 60:if test then else ;
** Pascal:if test then else ;
** C, Java: it (test) else ;
** Ada: if test then else end if;
** Fortran 77+:
* case/switch statement multi-way choice:
** Pascal:
** Ada:
** C, Java:
* 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 usedStatements 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 toNo 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). 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) andSemantics
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.Trouble spots in Algol 60 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, usingif
and goto
.
The 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 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 - Statements *References
External links