Loop structures: Difference between revisions

no edit summary
imported>Maleza
No edit summary
 
(4 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 382 ⟶ 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 408 ⟶ 452:
i = i+1
} while i<1000
</syntaxhighlight>
</lang>
 
 
==[[Groovy]]==
Anonymous user