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
ALGOL 68
(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!
===Expressions and compound statements=== ALGOL 68 being an [[expression-oriented programming languages|expression-oriented programming language]], the value returned by an [[assignment (programming)|assignment]] statement is a reference to the destination. Thus, the following is valid ALGOL 68 code: '''REAL''' half pi, one pi; one pi := 2 * ( half pi := 2 * arc tan(1) ) This notion is present in [[C (programming language)|C]] and [[Perl]], among others. Note that as in earlier languages such as [[Algol 60]] and [[FORTRAN]], spaces are allowed in identifiers, so that <code>half pi</code> is a ''single'' identifier (thus avoiding the ''underscores'' versus ''[[camel case (programming)|camel case]]'' versus ''all lower-case'' issues). As another example, to express the mathematical idea of a ''sum'' of <code>f(i)</code> from i=1 to n, the following ALGOL 68 ''integer expression'' suffices: ('''INT''' sum := 0; '''FOR''' i '''TO''' n '''DO''' sum +:= f(i) '''OD'''; sum) Note that, being an integer expression, the former block of code can be used in ''any context where an integer value can be used''. A block of code returns the value of the last expression it evaluated; this idea is present in [[Lisp (programming language)|Lisp]], among other languages. Compound statements are all terminated by distinctive closing brackets: *'''IF''' choice clauses: '''IF''' condition '''THEN''' statements [ '''ELSE''' statements ] '''FI''' "brief" form: ( condition | statements | statements ) '''IF''' condition1 '''THEN''' statements '''ELIF''' condition2 '''THEN''' statements [ '''ELSE''' statements ] '''FI''' "brief" form: ( condition1 | statements |: condition2 | statements | statements ) This scheme not only avoids the [[dangling else]] problem but also avoids having to use <code>'''BEGIN'''</code> and <code>'''END'''</code> in embedded [[Statement (programming)|statement]] sequences. *'''CASE''' choice clauses: '''CASE''' switch '''IN''' statements, statements,... [ '''OUT''' statements ] '''ESAC''' "brief" form: ( switch | statements,statements,... | statements ) '''CASE''' switch1 '''IN''' statements, statements,... '''OUSE''' switch2 '''IN''' statements, statements,... [ '''OUT''' statements ] '''ESAC''' "brief" form of '''CASE''' statement: ( switch1 | statements,statements,... |: switch2 | statements,statements,... | statements ) Choice clause example with ''Brief'' symbols: '''PROC''' days in month = ('''INT''' year, month)'''INT''': (month| 31, (year÷×4=0 ∧ year÷×100≠0 ∨ year÷×400=0 | 29 | 28 ), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ); Choice clause example with ''Bold'' symbols: '''PROC''' days in month = ('''INT''' year, month)'''INT''': '''CASE''' month '''IN''' 31, '''IF''' year '''MOD''' 4 '''EQ''' 0 '''AND''' year '''MOD''' 100 '''NE''' 0 '''OR''' year '''MOD''' 400 '''EQ''' 0 '''THEN''' 29 '''ELSE''' 28 '''FI''', 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 '''ESAC'''; Choice clause example mixing ''Bold'' and ''Brief'' symbols: '''PROC''' days in month = ('''INT''' year, month)'''INT''': '''CASE''' month '''IN''' ¢Jan¢ 31, ¢Feb¢ ( year '''MOD''' 4 = 0 '''AND''' year '''MOD''' 100 ≠ 0 '''OR''' year '''MOD''' 400 = 0 | 29 | 28 ), ¢Mar¢ 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ¢ to Dec. ¢ '''ESAC'''; Algol68 allowed the switch to be of either type '''INT''' ''or'' (uniquely) '''UNION'''. The latter allows the enforcing [[strong typing]] onto '''UNION''' variables. cf. '''[[#struct, union & .5B:.5D: Structures, unions and arrays|union]]''' below for example. *'''[[for loop|do]]''' loop clause: [ '''FOR''' index ] [ '''FROM''' first ] [ '''BY''' increment ] [ '''TO''' last ] [ '''WHILE''' condition ] '''DO''' statements '''OD''' The minimum form of a "loop clause" is thus: '''DO''' statements '''OD''' This was considered ''the'' "universal" loop, the full syntax is: '''FOR''' i '''FROM''' 1 '''BY''' -22 '''TO''' -333 '''WHILE''' i×i≠4444 '''DO''' ~ '''OD''' The construct have several unusual aspects: * only the '''''DO''' ~ '''OD''''' portion was compulsory, in which case the loop will iterate indefinitely. * thus the clause '''''TO''' 100 '''DO''' ~ '''OD''''', will iterate only 100 times. * the '''WHILE''' "syntactic element" allowed a programmer to break from a '''FOR''' loop early. e.g. '''INT''' sum sq:=0; '''FOR''' i '''WHILE''' print(("So far:",i,newline)); sum sq≠70↑2 '''DO''' sum sq+:=i↑2 '''OD''' Subsequent "extensions" to the standard Algol68 allowed the '''TO''' syntactic element to be replaced with '''UPTO''' and '''DOWNTO''' to achieve a small optimisation. The same compilers also incorporated: * '''UNTIL'''<sup>(C)</sup> – for late loop termination. * '''FOREACH'''<sup>(S)</sup> – for working on arrays in [[Parallel computing|parallel]]. Further examples can be found in the code examples below.
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)