Jump to content

Loops/For with a specified step: Difference between revisions

m
(Vedit macro language added)
Line 22:
end loop;
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 &and; f &le; t) &or; (b < 0 &and; f &ge; t) &or; 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 &ne; 70 &times; 70
'''do'''
sum sq +:= i &uarr; 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}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.