Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Statement (computer science)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Short description|A section of code that details a specific command}} In [[computer programming]], a '''statement''' is a [[Syntax (programming languages)|syntactic]] unit of an [[Imperative programming|imperative programming language]] that expresses some action to be carried out.<ref>{{cite web | url = http://www.webopedia.com/TERM/S/statement.html | title = statement | date = September 1996 | publisher = webopedia | access-date = 2015-03-03}}</ref> A [[Computer program|program]] written in such a language is formed by a sequence of one or more statements. A statement may have internal components (e.g. [[Expression (computer science)|expressions]]). Many programming languages (e.g. [[Ada (programming language)|Ada]], [[Algol 60]], [[C (programming language)|C]], [[Java (programming language)|Java]], [[Pascal (programming language)|Pascal]]) make a distinction between statements and [[Declaration (computer programming)|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''.<ref name=ALGOL60>{{cite web|url=http://www.masswerk.at/algol60/report.htm |at=Section "4.1" |title=Revised Report on the Algorithmic Language Algol 60 |first1=J.W. |last1=Backus |first2=F.L. |last2=Bauer |first3=J. |last3=Green |first4=C. |last4=Katz |first5=J. |last5=McCarthy |first6=P. |last6=Naur |first7=A.J. |last7=Perlis |first8=H. |last8=Rutishauser |first9=K. |last9=Samuelson |first10=B. |last10=Vauquois |first11=J.H. |last11=Wegstein |first12=A. |last12=van Wijngaarden |first13=M. |last13=Woodger |editor-first1= Peter |editor-last1=Naur |website=mass:werk |access-date=January 23, 2021}}</ref> The appearance of a statement (and indeed a program) is determined by its [[Syntax (programming languages)|syntax]] or grammar. The meaning of a statement is determined by its [[Semantics (computer science)|semantics]]. ==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 statement|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 (computer science)|assignment]] **Fortran: <code>''variable'' = ''expression''</code> **Pascal, Algol 60, Ada: <code>''variable'' := ''expression'';</code> **C, C#, C++, PHP, Java: <code>''variable'' = ''expression'';</code> * [[Subroutine|call]] **Fortran: <code>CALL ''subroutine name''(''parameters'')</code> **C, C++, Java, PHP, Pascal, Ada: <code>''subroutine name''(''parameters'');</code> * [[Assertion (software development)|assertion]] **C, C++, PHP: <code>assert(''relational expression'');</code> **Java: <code>assert ''relational expression'';</code> * [[GOTO|goto]] **Fortran: <code>GOTO numbered-label</code> **Algol 60: <code>'''goto''' ''label'';</code> **C, C++, PHP, Pascal: <code>goto ''label'';</code> * [[Return statement|return]] **Fortran: <code>RETURN ''value''</code> **C, C++, Java, PHP: <code>return ''value'';</code> * [[exit (system call)|stop/halt/exit]] **Fortran: <code>STOP ''number''</code> **C, C++: <code>exit(''expression'')</code> **PHP: <code>exit ''number'';</code> ==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: ::* {{tt|<statement>}} is any single statement (could be simple or compound). ::* {{tt|<sequence>}} is any sequence of zero or more {{tt|<statements>}} ::Some programming languages provide a general way of grouping statements together, so that any single {{tt|<statement>}} can be replaced by a group: ::* Algol 60: <code>'''begin''' <sequence> '''end'''</code> ::* Pascal: <code>begin <sequence> end</code> ::* C, PHP, Java: <code>{ <sequence> }</code> ::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: {{code|if test then <sequence> end if;|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. * [[For loop|count-controlled loop]]: ** Algol 60: <code>'''for''' index := 1 '''step''' 1 '''until''' limit '''do''' <statement> ;</code> ** Pascal: {{code|2=pascal|1=for index := 1 to limit do <statement> ;}} ** C, Java: {{code|2=c|1=for ( index = 1; index <= limit; index += 1) <statement> ;}} ** Ada: {{code|for index in 1..limit loop <sequence> end loop|ada}} ** Fortran 90:{{sxhl|2=fortran|1= DO index = 1,limit <sequence> END DO}} * [[While loop|condition-controlled loop]] with test at start of loop: ** Algol 60: <code>'''for''' index := expression '''while''' test '''do''' <statement> ;</code> ** Pascal: {{code|while test do <statement> ;|pascal}} ** C, Java: {{code|2=c|1=while (test) <statement> ;}} ** Ada: {{code|while test loop <sequence> end loop|ada}} ** Fortran 90: {{sxhl|2=fortran| DO WHILE (test) <sequence> END DO}} * [[Do while loop|condition-controlled loop]] with test at end of loop: ** Pascal: {{code|repeat <sequence> until test; { note reversed test }|pascal}} ** C, Java: {{code|do { <sequence> } while (test) ;|c}} ** Ada: {{code|loop <sequence> exit when test; end loop;|ada}} * condition-controlled loop with test in the middle of the loop: ** C: {{code|do { <sequence> if (test) break; <sequence> } while (true) ;|c}} ** Ada: {{code|loop <sequence> exit when test; <sequence> end loop;|ada}} * [[Conditional (programming)|if-statement]] simple situation: ** Algol 60:<code>'''if''' test '''then''' <unconditional statement> ;</code> ** Pascal: {{code|if test then <statement> ;|pascal}} ** C, Java: {{code|if (test) <statement> ;|c}} ** Ada: {{code|if test then <sequence> end if;|ada}} ** Fortran 77+: {{sxhl|2=fortran| IF (test) THEN <sequence> END IF}} * [[Conditional (programming)|if-statement]] two-way choice: ** Algol 60: <code>'''if''' test '''then''' <unconditional statement> '''else''' <statement> ;</code> ** Pascal: {{code|if test then <statement> else <statement> ;|pascal}} ** C, Java: {{code|if (test) <statement> else <statement> ;|c}} ** Ada: {{code|if test then <sequence> else <sequence> end if;|ada}} ** Fortran 77+: {{sxhl|2=fortran| IF (test) THEN <sequence> ELSE <sequence> END IF }} * [[Switch statement|case/switch statement]] multi-way choice: ** Pascal: {{code|2=pascal|case c of 'a': alert(); 'q': quit(); end;}} ** Ada: {{code|2=ada|1=case c is when 'a' => alert(); when 'q' => quit(); end case;}} ** C, Java: {{code|2=c|switch (c) { case 'a': alert(); break; case 'q': quit(); break; } }} * [[Exception handling]]: ** Ada: <code>begin ''protected code'' except when ''exception specification'' => ''exception handler''</code> ** Java: <code>try { ''protected code'' } catch (''exception specification'') { ''exception handler'' } finally { ''cleanup'' }</code> ** Python: <code> try: ''protected code'' except ''exception specification'': ''exception handler'' else: ''no exceptions'' finally: ''cleanup''</code> ==Syntax== {{main|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 used [[Backus–Naur form]] (BNF) which set a new level for language grammar specification.<ref name=ALGOL60RPT>{{cite web|url=http://www.masswerk.at/algol60/report.htm |at=Section "1.1" |title=Revised Report on the Algorithmic Language Algol 60 |first1=J.W. |last1=Backus |first2=F.L. |last2=Bauer |first3=J. |last3=Green |first4=C. |last4=Katz |first5=J. |last5=McCarthy |first6=P. |last6=Naur |first7=A.J. |last7=Perlis |first8=H. |last8=Rutishauser |first9=K. |last9=Samuelson |first10=B. |last10=Vauquois |first11=J.H. |last11=Wegstein |first12=A. |last12=van Wijngaarden |first13=M. |last13=Woodger |editor-first1= Peter |editor-last1=Naur |website=mass:werk |access-date=January 23, 2021}}</ref> * Up until Fortran 77, the language was described in English prose with examples,<ref name=FORTRAN66>{{cite web|url=https://wg5-fortran.org/ARCHIVE/Fortran66.pdf|title=FORTRAN |date=1966 |publisher=United States of America Standards Institute |via=WG5 Fortran Standards |access-date=February 19, 2021}}</ref> From Fortran 90 onwards, the language was described using a variant of BNF.<ref name=FORTRAN95>{{cite web|url=https://j3-fortran.org/doc/year/04/04-007.pdf|title=Working draft J3/04-007 |date=May 10, 2004 |publisher=J3 Fortran |access-date=February 19, 2021}}</ref> * Cobol used a two-dimensional metalanguage.<ref name=COBOL1959>{{cite web|url=https://public.support.unisys.com/2200/docs/CP18.0/PDF/78307709-002.pdf|title=ASCII COBOL Programming Reference Manual |date= June 2010 |publisher=unisys |access-date=January 23, 2021}}</ref> * Pascal used both [[syntax diagram]]s and equivalent BNF.<ref name=PASCAL>{{cite web|url=http://prog.vub.ac.be/~tjdhondt/ESL/Pascal_files/PASCAL%20user%20manual%20and%20report.pdf|title=PASCAL User Manual and Report |first1=Kathleen |last1=Jensen |first2=Niklaus |last2=Wirth |editor-first1=G. |editor-last1=Goos |editor-first2=J. |editor-last2=Hartmanis |date=1974 |work=Lecture Notes in Computer Science |at=Appendix D |access-date=February 19, 2021}}</ref> BNF uses recursion to express repetition, so various [[Extended Backus–Naur form|extensions]] have been proposed to allow direct indication of repetition. ===Statements and keywords=== Some programming language grammars [[reserved word|reserve keywords]] or [[Stropping (syntax)|mark them specially]], and do not allow them to be used as [[Identifier (computer languages)|identifiers]]. This often leads to [[Formal grammar|grammars]] which are easier to [[parsing|parse]], requiring less [[Parsing#Lookahead|lookahead]]. ====No distinguished keywords==== Fortran and PL/1 do not have reserved keywords, allowing statements like: * in PL/1: **<code>IF IF = THEN THEN ...</code> (the second <code>IF</code> and the first <code>THEN</code> are variables). * in Fortran: **<code>IF (A) X = 10... </code> conditional statement (with other variants) **<code>IF (A) = 2 </code> assignment to a subscripted variable named <code>IF</code> ::As spaces were optional up to Fortran 95, a typo could completely change the meaning of a statement: :*<code>DO 10 I = 1,5 </code> start of a loop with I running from 1 to 5 :*<code>DO 10 I = 1.5 </code> assignment of the value 1.5 to the variable <code>DO10I</code> ====Flagged words==== {{main|Stropping (syntax)}} In Algol 60 and Algol 68, special tokens were distinguished explicitly: for publication, in boldface e.g. <code>'''begin'''</code>; for programming, with some special marking, e.g., a flag (<code>'begin</code>), quotation marks (<code>'begin'</code>), or underlined (<code><u>begin</u></code> 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==== {{main|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 include [[FLOW-MATIC]] (1953) and [[COBOL]] (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== {{main|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.<ref name=Trouble>{{cite web|url=https://people.eecs.berkeley.edu/~necula/Papers/KnuthTroubleAlgol.pdf|title=The Remaining Trouble Spots in Algol 60 |first1=D. E. |last1=Knuth |date=Jul 1967 |work=The ALGOL Family |access-date=February 24, 2021}}</ref> 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 [[Label (computer science)|labels]], using <code>if</code> and <code>goto</code>. The [[Semantics (computer science)|semantics]] 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 [[expression (programming)|expression]]s, 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, <code>x = y + 1</code> 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, <code>x = y + 1;</code> (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.<ref>{{Cite web |title=ISO/IEC 9899:1999 (E) |url=https://www.dii.uchile.cl/~daespino/files/Iso_C_1999_definition.pdf |url-status=live |archive-url=https://web.archive.org/web/20240207035551/http://www.dii.uchile.cl/~daespino/files/Iso_C_1999_definition.pdf |archive-date=Feb 7, 2024 |website=ISO/IEC}}</ref> Expressions can also be contained within other expressions. For instance, the expression <code>x = y + 1</code> contains the expression <code>y + 1</code>, which in turn contains the values <code>y</code> and <code>1</code>, 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 (Programming Language)|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.<ref>{{cite web | url=https://docs.python.org/3/reference/simple_stmts.html#assignment-statements | title=7. Simple statements |work=Python 3.10.8 documentation }}</ref> ==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== * {{slink|Comparison of programming languages (syntax)#Statements}} * [[Control flow]] ==References== {{Reflist}} ==External links== * [https://www.pcmag.com/encyclopedia/term/program-statement PC ENCYCLOPEDIA: Definition of: program statement] {{Authority control}} {{DEFAULTSORT:Statement (Programming)}} [[Category:Programming language concepts]] [[Category:Statements]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Authority control
(
edit
)
Template:Cite web
(
edit
)
Template:Code
(
edit
)
Template:Main
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)
Template:Slink
(
edit
)
Template:Sxhl
(
edit
)
Template:Tt
(
edit
)