Anonymous user
Loop structures: Difference between revisions
no edit summary
imported>Maleza No edit summary |
|||
(6 intermediate revisions by 2 users not shown) | |||
Line 171:
for( i=0; i<9; i++)
cout ` $i\n`;
</syntaxhighlight>
=={{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>
Line 231 ⟶ 274:
===Looping===
Most looping is done with recursion. Tail recursion is properly optimized.
<syntaxhighlight lang="factor">
: forever ( quot -- ) dup slip forever ; inline
[ "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.
</syntaxhighlight>
Iterating with an index:
<syntaxhighlight lang="factor">
: indexed-alphabet. ( -- )
"abcdefghijklmnopqrstuvwxyz"
[ [ 1string ] [ number>string ] bi* ": " glue print ] each-index ;
</syntaxhighlight>
==[[Forth]]==
===DO-LOOP===
<syntaxhighlight lang="forth">
( limit start ) DO ( iterated statements ) ( increment ) +LOOP
LEAVE \ exits a DO loop
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 . 2 +LOOP \ Prints the even numbers from 0 to 8
</syntaxhighlight>
===BEGIN-UNTIL===
<syntaxhighlight lang="forth">
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 ;
</syntaxhighlight>
===BEGIN-AGAIN===
<syntaxhighlight lang="forth">
BEGIN ( iterated statements ) AGAIN
</syntaxhighlight>
example: echo user's input
<syntaxhighlight lang="forth">
: FOREVER ( -- ) BEGIN KEY EMIT AGAIN ;
</syntaxhighlight>
===BEGIN-WHILE-REPEAT===
<syntaxhighlight lang="forth">
BEGIN ( unconditional iterated statements ) ( conditional ) WHILE ( conditional iterated statements ) REPEAT
example: counts down from a given number to one
</syntaxhighlight>
Additional WHILE clauses may be added to a loop, but each extra WHILE requires a matching THEN after the REPEAT.
Line 287 ⟶ 355:
A good example of a useful combination is this complex loop:
<syntaxhighlight lang="forth">
BEGIN
( condition 1 )
WHILE
( condition 2 )
UNTIL
( condition 2 succeeded )
ELSE
( condition 1 failed )
THEN
</syntaxhighlight>
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 353 ⟶ 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.
<
for i = 1 to 1000000
{
println[i]
}
</syntaxhighlight>
The <CODE>to</CODE> operator can be combined with a <CODE>step</CODE> statement:
<
for i = 1 to 1000000 step 3
println[i]
</syntaxhighlight>
As a <CODE>foreach</CODE> statement. The <CODE>for</CODE> construct can iterate over the elements of an array, set, dictionary, or enumerating expression.
<
for i = [2,3,7,9]
println[i]
</syntaxhighlight>
===Do...While Loop===
<
i=0
do
Line 379 ⟶ 452:
i = i+1
} while i<1000
</syntaxhighlight>
==[[Groovy]]==
|