Loops/For with a specified step: Difference between revisions
Content added Content deleted
(FALSE) |
(→{{header|ALGOL 68}}: Rm custom highlighting and extra whitespace) |
||
Line 23: | Line 23: | ||
put("what's a word that rhymes with ""twelve""?");</lang> |
put("what's a word that rhymes with ""twelve""?");</lang> |
||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
The ALGOL 68 " |
The ALGOL 68 "universal" for/while loop: |
||
<lang algol68>[ for index ] [ from first ] [ by increment ] [ to last ] [ while condition ] do statements od</lang> |
|||
The minimum form of a "loop clause" is thus: |
|||
<lang algol68>do statements od # an infinite loop #</lang> |
|||
The formal specification of ALGOL 68 states: |
The formal specification of ALGOL 68 states: |
||
<lang algol68>for i from u1 by u2 to u3 while condition do action od</lang> |
|||
"is thus equivalent to the following void-closed-clause:" |
"is thus equivalent to the following void-closed-clause:" |
||
<lang algol68>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</lang> |
|||
There are several unusual aspects of the construct: |
There are several unusual aspects of the construct: |
||
** only the |
** only the 'do ~ od' portion was compulsory, in which case the loop will iterate indefinitely. |
||
** thus the clause |
** thus the clause 'to 100 do ~ od', will iterate only 100 times. |
||
** the |
** the while "syntactic element" allowed a programmer to break from a for loop early. eg |
||
<lang algol68>int sum sq:=0; |
|||
for i while |
|||
sum sq ≠ 70 × 70 |
|||
do |
|||
sum sq +:= i ↑ 2 |
|||
od</lang> |
|||
'''od''' |
|||
⚫ | |||
* until<sup>(C)</sup> - for late loop termination. |
|||
⚫ | |||
* |
* foreach<sup>(S)</sup> - for working on arrays in [[Parallel computing|parallel]]. |
||
* '''foreach'''<sup>(S)</sup> - for working on arrays in [[Parallel computing|parallel]]. |
|||
=={{header|BASIC}}== |
=={{header|BASIC}}== |