Statement (programming)

From Free net encyclopedia

Revision as of 13:45, 21 April 2006; view current revision
←Older revision | Newer revision→

In computer programming a statement can be thought of as the smallest standalone element of an imperative programming language. A program is formed by sequence of one or more statements. A statement will have internal components (eg, expressions).

Many languages (eg, C) make a distiction between statements and definitions, with a statement only containing executable code and a definition declaring an identifier.

The following are the major generic kinds of statements with examples in typical imperative languages:

  • definition: TYPE SALARY = INTEGER
  • declaration: VAR A:INTEGER
  • assignment: A := A + 1
  • sequence: A := A + 1; WRITELN(A)
  • statement block: begin WRITE('Number? '); READLN(NUMBER); end
  • if-statement: if A > 3 then WRITELN(A) else WRITELN("NOT YET") end
  • switch-statement: switch (c) { case 'a': alert(); break; case 'q': quit(); break; }
  • while-loop: while NOT EOF DO begin READLN end
  • do-loop: do { computation(&i); } while (i < 10);
  • for-loop: for A:=1 to 10 do WRITELN(A) end
  • call: CLEARSCREEN()
  • goto: goto 1
  • assertion: assert(ptr != NULL);

In most languages statements contrast with expressions in that the former do not return results and are executed solely for their side effects, while the latter always return a result and often do not have side effects at all. Among imperative programming languages, Algol 68 is one of the few in which a statement can return a result. In languages which 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'.

The syntax and semantics of statements is usually specified in the definition of the programming language and cannot be changed in a program. Only few programming languages allow user defined statements or overloading of statements.

Typical programming languages with statements are Ada, ALGOL, BASIC, C, [[C++]], COBOL, Eiffel, Fortran, Java, JavaScript, Modula, Oberon, Pascal, Perl, PL/I, Python, REXX, Ruby, Seed7, Simula and Tcl.

See also

Control flowru:Инструкция (программирование) vi:Câu lệnh