Loops/For with a specified step: Difference between revisions
Content added Content deleted
(Vedit macro language added) |
|||
Line 22: | Line 22: | ||
end loop; |
end loop; |
||
put("what's a word that rhymes with ""twelve""?");</lang> |
put("what's a word that rhymes with ""twelve""?");</lang> |
||
=={{header|ALGOL 68}}== |
|||
The</b> ALGOL 68 "universal" '''for'''/'''while''' loop: |
|||
[ '''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''' # an infinite loop # |
|||
The formal specification of ALGOL 68 states: |
|||
'''for''' i '''from''' u1 '''by''' u2 '''to''' u3 '''while''' condition '''do''' action '''od''' |
|||
"is thus equivalent to the following void-closed-clause:" |
|||
'''begin''' '''int''' f:= u1, '''int''' b = u2, t = u3; |
|||
step2: |
|||
'''if''' (b > 0 ∧ f ≤ t) ∨ (b < 0 ∧ f ≥ t) ∨ b = 0 |
|||
'''then''' '''int''' i = f; |
|||
'''if''' condition |
|||
'''then''' action; f +:= b; '''go''' '''to''' step2 |
|||
'''fi''' |
|||
'''fi''' |
|||
'''end''' |
|||
There are several unusual aspects of the construct: |
|||
** 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. eg |
|||
'''int''' sum sq:=0; |
|||
'''for''' i '''while''' |
|||
sum sq ≠ 70 × 70 |
|||
'''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]]. |
|||
=={{header|BASIC}}== |
=={{header|BASIC}}== |