Loop structures: Difference between revisions

no edit summary
(Loop Structures in FeeBASIC)
imported>Maleza
No edit summary
 
(17 intermediate revisions by 2 users not shown)
Line 27:
The below code snippet represents a loop that continues until a value greater than 3500 is read. However, it will also end after the 2000th iteration automatically, regardless of whether the condition is ever met.
 
<syntaxhighlight lang="68000devpac">
<lang 68000devpac>MOVE.W #1999,D1 ;DBxx loop counters need to be pre-decremented to work properly, since they terminate at $FFFF rather than 0
MOVE.W #1999,D1 ;DBxx loop counters need to be pre-decremented to work properly, since they terminate at $FFFF rather than 0
LOOP:
MOVE.W (A0)+,D0
CMP.W #3501,D0 ;COMPARE TO #3501
DBCC D1,LOOP ;DECREMENT, BRANCH UNTIL CARRY CLEAR OR D1 = #$FFFF</lang>
</syntaxhighlight>
 
=={{header|AmbientTalk}}==
 
===doTimes===
<langsyntaxhighlight lang="ambienttalk">
// print 1 2 3 ... 20
20.doTimes { |i| system.print(" "+i); }
</lang>
 
</syntaxhighlight>
=== each ===
 
===each===
Iterate over a collection:
 
Iterate over a collection:
<lang ambienttalk>
<syntaxhighlight lang="ambienttalk">
[ "foo", "bar", "baz" ].each: { |e|
system.print(" "+e);
}
// prints: foo bar baz
 
</lang>
</syntaxhighlight>
 
==[[AppleScript]]==
'''NOT COVERED IN LOOP PAGES'''
===repeat-until===
<syntaxhighlight lang="applescript>
set i to 5
set i to 5
repeat until i is less than 0
repeat setuntil i tois iless -than 10
set i to i - 1
end repeat
end repeat
 
repeat
--endless loop
end repeat
</syntaxhighlight>
 
===repeat-with===
<syntaxhighlight lang="applescript>
repeat with i from 1 to 20
repeat with i from 1 to 20
--do something
end--do repeatsomething
end repeat
 
set array to {1,2,3,4,5}
repeat with i in array
display dialog i
end repeat
 
set array to {1,2,3,4,5}
repeat with i in array
display dialog i
end repeat
</syntaxhighlight>
 
==[[AssemblyScript]]==
Line 79 ⟶ 85:
 
===while===
<langsyntaxhighlight lang="javascript">
let done = false
while (!done) {
done = true
}
</syntaxhighlight>
</lang>
 
===do while===
<langsyntaxhighlight lang="javascript">
let done = false
do {
done = true
} while (!done)
</syntaxhighlight>
</lang>
 
===for===
<langsyntaxhighlight lang="javascript">
for (let i = 0; i < 10000; i++) {
i += i
}
</syntaxhighlight>
</lang>
 
==[[Brainf***]]==
Line 116 ⟶ 122:
A compile-time for loop can be generated with template metaprogramming. Example:
 
<syntaxhighlight lang="cpp">
// the loop
// the loop
template<int start, int finish, template<int n, typename T> class X> struct loop
template<int start, int finish, template<int n, typename T> class X> struct loop
{
{
typedef typename X<start, typename loop<start+1, finish, X>::type>::type type;
typedef typename X<start, typename loop<start+1, finish, X>::type>::type type;
};
};
// the termination of the loop
template<int finish, template<int n, typename T> class X> struct loop<finish, finish, X>
{
typedef typename X<finish, void>::type type;
};
 
// example usage: This implements just a very complicated way of building a multi-dimensional array
// the loop body
template<int n, typename T> struct build_array
{
typedef T type[n];
};
template<int n> struct build_array<n, void>
{
typedef double type;
};
// the loop execution: This is equivalent to
// typedef double array_type[2][3][4][5];
typedef loop<2,6,build_array>::type array_type;
</syntaxhighlight>
 
==[[Clojure]]==
'''NOT COVERED IN LOOP PAGES'''
===loop===
<langsyntaxhighlight lang="clojure">
;; loop/recur is the most general looping construct
(loop [lst [1 3 5 7]
ret []]
Line 156 ⟶ 164:
ret))
==> [1 9 25 49]
</syntaxhighlight>
</lang>
 
=={{header|Crack}}==
===For===
<langsyntaxhighlight lang="crack">
for( i=0; i<9; i++)
cout ` $i\n`;
</syntaxhighlight>
</lang>
 
=={{header|Curto}}==
===HACER-BUCLE===
<syntaxhighlight lang="curto">
\ limite inicio HACER sentencias iteradas BUCLE
\ limite inicio HACER sentencias iteradas incremento +BUCLE
\ SALIR \ abandona bucle HACER
\ DBUCLE SALIR \ limpia contadores de la pila de retorno antes de abandonar la palabra actual
</syntaxhighlight>
 
ejemplo: Dos iteraciones típicas
<syntaxhighlight lang="curto">
10 0 hacer i . bucle \ Imprime números de 0 a 9
10 0 hacer i . 2 +bucle \ Imprime números pares de 0 a 8
</syntaxhighlight>
===EMPEZAR-HASTA===
<syntaxhighlight lang="curto">
\ EMPEZAR sentencias iteradas condicional HASTA
</syntaxhighlight>
 
ejemplo: Cuenta hacia abajo desde un número dado a cero
<syntaxhighlight lang="curto">
: cuenta-abajo ( n -- ) empezar dup rc . 1- dup 0< hasta soltar ;
</syntaxhighlight>
 
===EMPEZAR-DENUEVO===
<syntaxhighlight lang="curto">
\ EMPEZAR sentencias iteradas DENUEVO
</syntaxhighlight>
 
ejemplo: repetir entrada de usuario (solo funciona en cli, no en la interface gráfica)
<syntaxhighlight lang="curto">
: porsiempre ( -- ) empezar tecla emitir denuevo ;
</syntaxhighlight>
 
===EMPEZAR-MIENTRAS-REPETIR===
<syntaxhighlight lang="curto">
\ EMPEZAR sentencias iteradas incondicionales condicional MIENTRAS sentencias iteradas condicionales repetir
</syntaxhighlight>
ejemplo: cuenta hacia abajo desde un número dado a uno
<syntaxhighlight>
: cuenta-abajo ( n -- ) empezar dup mientras rc dup . 1- repetir soltar ;
</syntaxhighlight>
 
=={{header|Dafny}}==
<langsyntaxhighlight lang="dafny">
var i: int := 0;
while i < n
Line 175 ⟶ 226:
}
assert i == n;
</syntaxhighlight>
</lang>
 
=={{header|Dao}}==
===For===
<syntaxhighlight lang="java">
<lang java>for( i=0; i<9; ++i) io.writeln( i );
for( i = 0; : 8i<9; ++i) io.writeln( i );</lang>
for( i = 0 : 8 ) io.writeln( i );</syntaxhighlight>
 
===For In===
<syntaxhighlight lang ="java">items = { 1, 2, 3 }
items = { 1, 2, 3 }
for( item in items ) io.writeln( item )</lang>
for( item in items ) io.writeln( item )
</syntaxhighlight>
 
===While===
<syntaxhighlight lang ="java">i = 0
i = 0
while( i < 5 ) { i += 1 }</lang>
while( i < 5 ) { i += 1 }
</syntaxhighlight>
 
===Do While===
<syntaxhighlight lang ="java">i = 0
i = 0
do { i += 1 } while( i < 9 )</lang>
do { i += 1 } while( i < 9 )
</syntaxhighlight>
 
=={{header|Déjà Vu}}==
===For===
Déjà Vu has a for-loop protocol, so you can write your own iterators. The most commonly used iterators are <code>in</code> and <code>range</code>. The first iterates over a list, the second takes two arguments and goes from the first to the second, like a classic for-loop.
<syntaxhighlight lang ="dejavu">for i range 1 3:
for i range 1 3:
!print i # prints 1, 2 and 3</lang>
!print i # prints 1, 2 and 3
</syntaxhighlight>
===While===
<syntaxhighlight lang ="dejavu">while true:
while true:
!print "This is the song that never ends..."</lang>
!print "This is the song that never ends..."
</syntaxhighlight>
===Repeat===
<syntaxhighlight lang ="dejavu">repeat 3:
repeat 3:
!print "This sentence is printed three times."</lang>
!print "This sentence is printed three times."
</syntaxhighlight>
 
==[[Factor]]==
Line 210 ⟶ 274:
===Looping===
Most looping is done with recursion. Tail recursion is properly optimized.
<syntaxhighlight lang="factor">
: forever ( quot -- ) dup slip forever ; inline
: forever ( quot -- ) dup slip forever ; inline
[ "A hungry raptor stalks you..." print flush 2000 random sleep ] forever
[ "A hungry raptor stalks you..." print flush 2000 random sleep ] forever
</syntaxhighlight>
 
===Iteration===
<syntaxhighlight lang="factor">
Most indices are implicit or not present at all.
3 [ "pint" drink ] times
{ "high" "level" "language" } [ print ] each
high
level
language
10 [ sq ] map
{ 0 1 4 9 16 25 36 49 64 81 }
{ 1 2 3 } { 4 5 6 } [ * ] 2map .
{ 4 10 18 }
10 [ even? ] subset .
V{ 0 2 4 6 8 }
0 10 3 <range> >array .
{ 0 3 6 9 }
10 1 -2 <range> >array .
{ 10 8 6 4 2 }
2222 [ dup 0 > ] [ 2/ dup ] [ ] unfold nip .
{ 1111 555 277 138 69 34 17 8 4 2 1 0 }a
</syntaxhighlight>
 
Iterating with an index:
<syntaxhighlight lang="factor">
: indexed-alphabet. ( -- )
: indexed-alphabet. ( -- )
"abcdefghijklmnopqrstuvwxyz"
"abcdefghijklmnopqrstuvwxyz"
[ [ 1string ] [ number>string ] bi* ": " glue print ] each-index ;
[ [ 1string ] [ number>string ] bi* ": " glue print ] each-index ;
</syntaxhighlight>
 
==[[Forth]]==
===DO-LOOP===
<syntaxhighlight lang="forth">
( limit start ) DO ( iterated statements ) LOOP
( limit start ) DO ( iterated statements ) ( increment ) +LOOP
( limit start ) DO ( iterated statements ) ( increment ) +LOOP
LEAVE \ exits a DO loop
LEAVE \ exits a DO loop
UNLOOP EXIT \ cleans up loop counters from return stack before returning from the current word
UNLOOP EXIT \ cleans up loop counters from return stack before returning from the current word
</syntaxhighlight>
 
example: Two standard iterations
<syntaxhighlight lang="forth">
10 0 DO I . LOOP \ Prints the numbers from 0 to 9
10 0 DO I . 2 +LOOP \ Prints the even numbers from 0 to 89
10 0 DO I . 2 +LOOP \ Prints the even numbers from 0 to 8
</syntaxhighlight>
 
===BEGIN-UNTIL===
<syntaxhighlight lang="forth">
BEGIN ( iterated statements ) ( conditional ) UNTIL
BEGIN ( iterated statements ) ( conditional ) UNTIL
</syntaxhighlight>
 
example: Counts down from a given number to zero
<syntaxhighlight lang="forth">
: COUNTDOWN ( n -- ) BEGIN DUP CR . 1- DUP 0< UNTIL DROP ;
: COUNTDOWN ( n -- ) BEGIN DUP CR . 1- DUP 0< UNTIL DROP ;
</syntaxhighlight>
 
===BEGIN-AGAIN===
<syntaxhighlight lang="forth">
BEGIN ( iterated statements ) AGAIN
BEGIN ( iterated statements ) AGAIN
</syntaxhighlight>
 
example: echo user's input
<syntaxhighlight lang="forth">
: FOREVER ( -- ) BEGIN KEY EMIT AGAIN ;
: FOREVER ( -- ) BEGIN KEY EMIT AGAIN ;
</syntaxhighlight>
 
===BEGIN-WHILE-REPEAT===
<syntaxhighlight lang="forth">
BEGIN ( unconditional iterated statements ) ( conditional ) WHILE ( conditional iterated statements ) REPEAT
BEGIN ( unconditional iterated statements ) ( conditional ) WHILE ( conditional iterated statements ) REPEAT
example: counts down from a given number to one
: COUNTDOWN ( n -- ) BEGIN DUP WHILE CR DUP . 1- REPEAT DROP ;
</syntaxhighlight>
Additional WHILE clauses may be added to a loop, but each extra WHILE requires a matching THEN after the REPEAT.
 
Line 266 ⟶ 355:
 
A good example of a useful combination is this complex loop:
<syntaxhighlight lang="forth">
BEGIN
BEGIN
( condition 1 )
( condition 1 )
WHILE
WHILE
( condition 2 )
( condition 2 )
UNTIL
UNTIL
( condition 2 succeeded )
( condition 2 succeeded )
ELSE
ELSE
( condition 1 failed )
( condition 1 failed )
THEN
THEN
An example of using this idiom in practice might be this pseudo-Forth
</syntaxhighlight>
BEGIN
read-next-record
WHILE
found-record
UNTIL
process-record
ELSE
error" Ran out of records looking for the right one!"
THEN
 
An example of using this idiom in practice might be this pseudo-Forth
<syntaxhighlight lang="forth">
BEGIN
read-next-record
WHILE
found-record
UNTIL
process-record
ELSE
error" Ran out of records looking for the right one!"
THEN
</syntaxhighlight>
 
=={{header|FreeBASIC}}==
Line 332 ⟶ 425:
===For Loop===
A <CODE>for</CODE> loop is really a <CODE>foreach</CODE> loop that can work with range operators or iterate through various data structures. The <CODE>to</CODE> operator creates an enumerating expression that lazily steps through its range.
<langsyntaxhighlight lang="frink">
for i = 1 to 1000000
{
println[i]
}
 
</lang>
</syntaxhighlight>
 
The <CODE>to</CODE> operator can be combined with a <CODE>step</CODE> statement:
<langsyntaxhighlight lang="frink">
for i = 1 to 1000000 step 3
println[i]
</syntaxhighlight>
</lang>
 
As a <CODE>foreach</CODE> statement. The <CODE>for</CODE> construct can iterate over the elements of an array, set, dictionary, or enumerating expression.
<langsyntaxhighlight lang="frink">
for i = [2,3,7,9]
println[i]
</syntaxhighlight>
</lang>
 
===Do...While Loop===
<langsyntaxhighlight lang="frink">
i=0
do
Line 358 ⟶ 452:
i = i+1
} while i<1000
</syntaxhighlight>
</lang>
 
 
==[[Groovy]]==
Anonymous user