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)
(section)
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!
==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>
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)