Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Statement (computer science)

From Wikipedia, the free encyclopedia
(Redirected fromStatement (programming))
Section of code that details a specific command
For other uses, seeStatement.

Incomputer programming, astatement is asyntactic unit of animperative programming language that expresses some action to be carried out.[1][vague] Aprogram 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)[2]: 15  make a distinction between statements anddefinitions/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 aresimple; those which can contain other statements arecompound.[3]

The appearance of a statement (and indeed a program) is determined by itssyntax or grammar. The meaning of a statement is determined by itssemantics.

Simple statements

[edit]

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;
  • call
    • Fortran:CALLsubroutine name(parameters)
    • C, C++, Java, PHP, Pascal, Ada:subroutine name(parameters);
  • assertion
    • C, C++, PHP:assert(relational expression);
    • Java:assertrelational expression;
  • goto
    • Fortran:GOTO numbered-label
    • Algol 60:gotolabel;
    • C, C++, PHP, Pascal:gotolabel;
  • return
    • Fortran:RETURNvalue
    • C, C++, Java, PHP:returnvalue;
  • stop/halt/exit
    • Fortran:STOPnumber
    • C, C++:exit(expression)
    • PHP:exitnumber;

Compound statements

[edit]
Main article:Block (programming)

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:
  • Algol 60:begin <sequence>end
  • Pascal:begin <sequence> end
  • 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:
  • Ada:iftestthen<sequence>endif;

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 := 1step 1until limitdo <statement> ;
    • Pascal:forindex:=1tolimitdo<statement>;
    • C, Java:for(index=1;index<=limit;index+=1)<statement>;
    • Ada:forindexin1..limitloop<sequence>endloop
    • Fortran 90:
      DOindex=1,limit<sequence>END DO
  • condition-controlled loop with test at start of loop:
    • Algol 60:for index := expressionwhile testdo <statement> ;
    • Pascal:whiletestdo<statement>;
    • C, Java:while(test)<statement>;
    • Ada:whiletestloop<sequence>endloop
    • Fortran 90:
      DO WHILE(test)<sequence>END DO
  • condition-controlled loop with test at end of loop:
    • Pascal:repeat<sequence>untiltest;{ note reversed test }
    • C, Java:do{<sequence>}while(test);
    • Ada:loop<sequence>exitwhentest;endloop;
  • condition-controlled loop with test in the middle of the loop:
    • C:do{<sequence>if(test)break;<sequence>}while(true);
    • Ada:loop<sequence>exitwhentest;<sequence>endloop;
  • if-statement simple situation:
    • Algol 60:if testthen <unconditional statement> ;
    • Pascal:iftestthen<statement>;
    • C, Java:if(test)<statement>;
    • Ada:iftestthen<sequence>endif;
    • Fortran 77+:
      IF(test)THEN<sequence>END IF
  • if-statement two-way choice:
    • Algol 60:if testthen <unconditional statement>else <statement> ;
    • Pascal:iftestthen<statement>else<statement>;
    • C, Java:if(test)<statement>else<statement>;
    • Ada:iftestthen<sequence>else<sequence>endif;
    • Fortran 77+:
      IF(test)THEN<sequence>ELSE<sequence>END IF
  • case/switch statement multi-way choice:
    • Pascal:casecof'a':alert();'q':quit();end;
    • Ada:caseciswhen'a'=>alert();when'q'=>quit();endcase;
    • C, Java:switch(c){case'a':alert();break;case'q':quit();break;}
  • Exception handling:
    • Ada:beginprotected code except whenexception specification =>exception handler
    • Java:try {protected code } catch (exception specification) {exception handler } finally {cleanup }
    • Python: try:protected code exceptexception specification:exception handler else:no exceptions finally:cleanup

Syntax

[edit]
Main article:Syntax (programming languages)

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 usedBackus–Naur form (BNF) which set a new level for language grammar specification.[4]
  • Up until Fortran 77, the language was described in English prose with examples,[5] From Fortran 90 onwards, the language was described using a variant of BNF.[6]
  • Cobol used a two-dimensional metalanguage.[7]
  • Pascal used bothsyntax diagrams and equivalent BNF.[8]

BNF uses recursion to express repetition, so variousextensions have been proposed to allow direct indication of repetition.

Statements and keywords

[edit]

Some programming language grammarsreserve keywords ormark them specially, and do not allow them to be used asidentifiers. This often leads togrammars which are easier toparse, requiring lesslookahead.

No distinguished keywords

[edit]

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 5
  • DO 10 I = 1.5 assignment of the value 1.5 to the variableDO10I

Flagged words

[edit]
Main article:Stropping (syntax)

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 theElliott 503). This is called "stropping".

Tokens that are part of the language syntax thus do not conflict with programmer-defined names.

Reserved keywords

[edit]
Main article:Reserved word

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 includeFLOW-MATIC (1953) andCOBOL (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

[edit]
Main article:Semantics (computer science)

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.[9] 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, andlabels, usingif andgoto.

Thesemantics 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

[edit]

A distinction is often made between statements, which are executed, andexpressions, 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.[10]

Expressions can also be contained within other expressions. For instance, the expressionx = y + 1 contains the expressiony + 1, which in turn contains the valuesy and1, 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 isPython, 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.[11]

Extensibility

[edit]

Most languages have a fixed set of statements defined by the language, but there have been experiments withextensible languages that allow the programmer to define new statements.

See also

[edit]

References

[edit]
  1. ^"statement". webopedia. September 1996. Retrieved2015-03-03.
  2. ^Dahl, Ole-Johan; Myhrhaug, Bjørn;Nygaard, Kristen (1970).Common Base Language(PDF) (Report). Norwegian Computing Center. Archived from the original on 2024-09-19. Retrieved20 August 2025.
  3. ^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". RetrievedJanuary 23, 2021.
  4. ^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". RetrievedJanuary 23, 2021.
  5. ^"FORTRAN"(PDF). United States of America Standards Institute. 1966. RetrievedFebruary 19, 2021 – via WG5 Fortran Standards.
  6. ^"Working draft J3/04-007"(PDF). J3 Fortran. May 10, 2004. RetrievedFebruary 19, 2021.
  7. ^"ASCII COBOL Programming Reference Manual"(PDF). unisys. June 2010. RetrievedJanuary 23, 2021.
  8. ^Jensen, Kathleen; Wirth, Niklaus (1974). Goos, G.; Hartmanis, J. (eds.)."PASCAL User Manual and Report"(PDF).Lecture Notes in Computer Science. Appendix D. RetrievedFebruary 19, 2021.
  9. ^Knuth, D. E. (Jul 1967)."The Remaining Trouble Spots in Algol 60"(PDF).The ALGOL Family. RetrievedFebruary 24, 2021.
  10. ^"ISO/IEC 9899:1999 (E)"(PDF).ISO/IEC.Archived(PDF) from the original on Feb 7, 2024.
  11. ^"7. Simple statements".Python 3.10.8 documentation.

External links

[edit]
Authority control databasesEdit this at Wikidata
Retrieved from "https://en.wikipedia.org/w/index.php?title=Statement_(computer_science)&oldid=1329835210"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp