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
Control flow
(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!
== Proposed control structures == In a spoof [[Datamation]] article<ref>[http://www.fortran.com/fortran/come_from.html We don't know where to GOTO if we don't know where we've COME FROM. This (spoof) linguistic innovation lives up to all expectations.] {{Webarchive|url=https://web.archive.org/web/20180716171336/http://www.fortran.com/fortran/come_from.html |date=2018-07-16 }} By R. Lawrence Clark* From Datamation, December, 1973</ref> in 1973, R. Lawrence Clark suggested that the GOTO statement could be replaced by the [[COMEFROM]] statement, and provides some entertaining examples. COMEFROM was implemented in one [[esoteric programming language]] named [[INTERCAL]]. [[Donald Knuth]]'s 1974 article "Structured Programming with go to Statements",<ref>Knuth, Donald E. "Structured Programming with go to Statements" [[ACM Computing Surveys]] 6(4):261-301, December 1974.</ref> identifies two situations which were not covered by the control structures listed above, and gave examples of control structures which could handle these situations. Despite their utility, these constructs have not yet found their way into mainstream programming languages. === Loop with test in the middle === The following was proposed by [[Ole-Johan Dahl|Dahl]] in 1972:<ref>Dahl & Dijkstra & Hoare, "Structured Programming" Academic Press, 1972.</ref> '''loop''' '''loop''' xxx1 read(char); '''while''' test; '''while''' '''not''' atEndOfFile; xxx2 write(char); '''repeat'''; '''repeat'''; If ''xxx1'' is omitted, we get a loop with the test at the top (a traditional '''while''' loop). If ''xxx2'' is omitted, we get a loop with the test at the bottom, equivalent to a '''do while''' loop in many languages. If '''while''' is omitted, we get an infinite loop. The construction here can be thought of as a '''do''' loop with the while check in the middle. Hence this single construction can replace several constructions in most programming languages. Languages lacking this construct generally emulate it using an equivalent infinite-loop-with-break idiom: '''while''' (true) { xxx1 '''if''' ('''not''' test) '''break''' xxx2 } A possible variant is to allow more than one '''while''' test; within the loop, but the use of '''exitwhen''' (see next section) appears to cover this case better. In [[Ada (programming language)|Ada]], the above loop construct ('''loop'''-'''while'''-'''repeat''') can be represented using a standard infinite loop ('''loop''' - '''end loop''') that has an '''exit when''' clause in the middle (not to be confused with the '''exitwhen''' statement in the following section). <syntaxhighlight lang="ada"> with Ada.Text_IO; with Ada.Integer_Text_IO; procedure Print_Squares is X : Integer; begin Read_Data : loop Ada.Integer_Text_IO.Get(X); exit Read_Data when X = 0; Ada.Text IO.Put (X * X); Ada.Text IO.New_Line; end loop Read_Data; end Print_Squares; </syntaxhighlight> Naming a loop (like ''Read_Data'' in this example) is optional but permits leaving the outer loop of several nested loops. === Multiple early exit/exit from nested loops === [[Zahn's construct|This construct]] was proposed by [[Zahn's construct|Zahn]] in 1974.<ref>Zahn, C. T. "A control statement for natural top-down structured programming" presented at Symposium on Programming Languages, Paris, 1974.</ref> A modified version is presented here. '''exitwhen''' EventA '''or''' EventB '''or''' EventC; xxx '''exits''' EventA: actionA EventB: actionB EventC: actionC '''endexit'''; '''exitwhen''' is used to specify the events which may occur within ''xxx'', their occurrence is indicated by using the name of the event as a statement. When some event does occur, the relevant action is carried out, and then control passes just after '''{{not a typo|endexit}}'''. This construction provides a very clear separation between determining that some situation applies, and the action to be taken for that situation. '''exitwhen''' is conceptually similar to [[exception handling]], and exceptions or similar constructs are used for this purpose in many languages. The following simple example involves searching a two-dimensional table for a particular item. '''exitwhen''' found '''or''' missing; '''for''' I := 1 '''to''' N '''do''' '''for''' J := 1 '''to''' M '''do''' '''if''' table[I,J] = target '''then''' found; missing; '''exits''' found: print ("item is in table"); missing: print ("item is not in table"); '''endexit''';
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)