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!
=== 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.
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)