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 "''universal''" '''for'''/'''while''' loop:
The ALGOL 68 "universal" for/while loop:
[ '''for''' index ] [ '''from''' first ] [ '''by''' increment ] [ '''to''' last ] [ '''while''' condition ] '''do''' statements '''od'''
<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: '''do''' statements '''od''' # an infinite loop #
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:
'''for''' i '''from''' u1 '''by''' u2 '''to''' u3 '''while''' condition '''do''' action '''od'''
<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:"
'''begin''' '''int''' f:= u1, '''int''' b = u2, t = u3;
<lang algol68>begin int f:= u1, int b = u2, t = u3;
step2:
step2:
'''if''' (b > 0 &and; f &le; t) &or; (b < 0 &and; f &ge; t) &or; b = 0
if (b > 0 &and; f &le; t) &or; (b < 0 &and; f &ge; t) &or; b = 0
'''then''' '''int''' i = f;
then int i = f;
'''if''' condition
if condition
'''then''' action; f +:= b; '''go''' '''to''' step2
then action; f +:= b; go to step2
'''fi'''
fi
'''fi'''
fi
'''end'''
end</lang>

There are several unusual aspects of the construct:
There are several unusual aspects of the construct:
** only the ''''''do''' ~ '''od'''''' portion was compulsory, in which case the loop will iterate indefinitely.
** 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.
** 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
** the while "syntactic element" allowed a programmer to break from a for loop early. eg
'''int''' sum sq:=0;
<lang algol68>int sum sq:=0;
'''for''' i '''while'''
for i while
sum sq &ne; 70 &times; 70
sum sq &ne; 70 &times; 70
'''do'''
do
sum sq +:= i &uarr; 2
sum sq +:= i &uarr; 2
od</lang>
'''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.
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]].
* '''foreach'''<sup>(S)</sup> - for working on arrays in [[Parallel computing|parallel]].


=={{header|BASIC}}==
=={{header|BASIC}}==