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.

A distinction can also be made between simple and compound statements; the latter may contain statements as components. The Algol 60 Report[2] provides formal descriptions and examples of both simple statements and compound statements.

Simple statements

Simple statements are complete in themselves; these include assignments, subroutine calls, assertions, 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 are handled by special statements, while other languages use calls to predefined subroutines.

  • assignment
    • in Fortran: A = A + 5
    • in Pascal, Algol 60, Ada: A := A + 5;
    • in C, PHP, Java: A = A + 5;
  • call
    • in Fortran: CALL CLEARSCREEN()
    • in C, Java, PHP, Pascal, Ada: ClearScreen();
  • assertion
    • in C, PHP: assert(ptr != NULL);
    • in Java: assert ptr != NULL;
  • goto
    • in Fortran: GOTO numbered-label
    • in Algol 60: goto named-label;
    • in C, PHP, Pascal: goto named-label;
  • return
    • in Fortran: RETURN value
    • in C, Java, PHP: return value;

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:

  • in Algol 60: begin <sequence> end
  • in Pascal: begin <sequence> end
  • in C, PHP, Java: { <sequence> }

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:

  • in Ada: if test then <sequence> end if;

Compound commands are either 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:
    • in Algol 60: for index := 1 step 1 until limit do <statement> ;
    • in Pascal: for index := 1 to limit do <statement> ;
    • in C, Java: for ( index = 1; index <= limit; index += 1) <statement> ;
    • in Ada: for index in 1..limit loop <sequence> end loop
    • in Fortran 90:
DO index = 1,limit
<sequence>
END DO
  • condition-controlled loop with test at start of loop:
    • in Algol 60: for index := expression while test do <statement> ;
    • in Pascal: while test do <statement> ;
    • in C, Java: while (test) <statement> ;
    • in Ada: while test loop <sequence> end loop
    • in Fortran 90:
DO WHILE (test)
<sequence>
END DO
  • condition-controlled loop with test at end of loop:
    • in Pascal: repeat <sequence> until test; { note reversed test}
    • in C, Java: do { <sequence> } while (test) ;
    • in Ada: loop <sequence> exit when test; end loop;
  • condition-controlled loop with test in the middle of the loop:
    • in C: do { <sequence> if (test) break; <sequence> } while (true) ;
    • in Ada: loop <sequence> exit when test; <sequence> end loop;
  • if-statement simple situation:
    • in Algol 60:if test then <unconditional statement> ;
    • in Pascal:if test then <statement> ;
    • in C, Java: it (test) <statement> ;
    • in Ada: if test then <sequence> end if;
    • in Fortran 77+:
IF (test) THEN
<sequence>
END IF
  • if-statement two-way choice:
    • in Algol 60:if test then <unconditional statement> else <statement> ;
    • in Pascal:if test then <statement> else <statement> ;
    • in C, Java: it (test) <statement> else <statement> ;
    • in Ada: if test then <sequence> else <sequence> end if;
    • in Fortran 77+:
IF (test) THEN
<sequence>
ELSE
<sequence>
END IF
  • case/switch statement multi-way choice:
    • in Pascal: case c of 'a': alert(); 'q': quit(); end;
    • in Ada: case c is when 'a' => alert(); when 'q' => quit(); end case;
    • in C, Java: switch (c) { case 'a': alert(); break; case 'q': quit(); break; }

Syntax

The appearance of statements shapes the look of programs. Programming languages are characterized by the type of statements they use (e.g. the curly brace language family). Many statements are introduced by keywords like if, while or repeat. Most languages since the 1960s have keywords are reserved such that they cannot be used as names of variables or functions. Imperative languages typically use special syntax for each statement, which looks quite different from function calls. Common methods to describe the syntax of statements are Backus–Naur form and syntax diagrams.

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. This leads to statements like the following.

  • in PL/1:
    • IF IF = THEN THEN ... (the second IF and the first THEN refer to variables).
  • in Fortran 77:
    • IF (A) 1,2 is a two-way branch (A=true, A=false)
    • IF (A) 1,2,3 is a three way branch (A<0, A=0, A>0)
    • IF (A) X=F(1,2) is a conditional assignment if A is true
    • IF (A) = 2 is an assignment to a subscripted variable named IF
    • IF (A) THEN is the start of a conditional compound statement

In addition, spaces were formerly optional in Fortran (up until Fortran 95), so a simple typo could completely change the meaning of a statement:

    • DO 10 I = 1,5 is the start of a loop with I running from 1 to 5 inclusive
    • DO 10 I = 1.5 is an assignment of the value 1.5 to the new 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 variable or other programmer-defined names.

Reserved keywords

Most languages since the early 1960s, including Ada, C, C++, Java, and Pascal, reserve certain tokens as part of the language syntax.

Semantics

Semantically many statements differ from subroutine calls by their handling of parameters. Usually an actual subroutine parameter is evaluated once before the subroutine is called. This contrasts to many statement parameters that can be evaluated several times (e.g. the condition of a while loop) or not at all (e.g. the loop body of a while loop). Technically such statement parameters are call-by-name parameters. Call-by-name parameters are evaluated when needed (see also lazy evaluation). When call-by-name parameters are available a statement like behaviour can be implemented with subroutines (see Lisp). For languages without call-by-name parameters the semantic description of a loop or conditional is usually beyond the capabilities of the language. Therefore, standard documents often refer to semantic descriptions in natural language.

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

  1. "statement". webopedia. Retrieved 2015-03-03.
  2. Revised ALGOL 60 report section. 4.1."ALGOL 60". Retrieved January 23, 2021.
This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.