Loop structures: Difference between revisions

no edit summary
imported>Maleza
No edit summary
 
(5 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 267 ⟶ 310:
==[[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 295 ⟶ 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
</syntaxhighlight>
 
An example of using this idiom in practice might be this pseudo-Forth
<syntaxhighlight lang="forth">
BEGIN
BEGIN
read-next-record
read-next-record
WHILE
WHILE
found-record
found-record
UNTIL
UNTIL
process-record
process-record
ELSE
ELSE
error" Ran out of records looking for the right one!"
error" Ran out of records looking for the right one!"
THEN
THEN
 
</syntaxhighlight>
 
=={{header|FreeBASIC}}==
Line 361 ⟶ 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 387 ⟶ 452:
i = i+1
} while i<1000
</syntaxhighlight>
</lang>
 
 
==[[Groovy]]==
Anonymous user