PARA... SIGUIENTES Declaraciones

Objetivo:

Ejecutar una serie de instrucciones un número especificado de tiempos en un lazo.

Sintaxis:

FOR variable=x TO y [STEP z] 
. 
. 
. 
NEXT [variable][,variable...]

Comentarios:

variable es usada como un contador.

x, y, y z son expresiones numéricas.

STEP z especifica el incremento contrario para cada lazo.

La primera expresión (x) numérica es el valor inicial del contador. La segunda expresión numérica (y) es el valor final del contador.

Las líneas de programa después FOR la declaración son ejecutadas hasta que NEXT declaración sea encontrada. Entonces, el contador es incrementado por la cantidad especificada por STEP.

Si STEP no es especificado, se asume que el incremento es 1.

Un control es realizado para ver si el valor del contador es mayor ahora que el valor final (y). Si no es mayor, ramas de GW-BASIC atrás a la declaración después el PARA la declaración, y el proceso es repetido. Si es mayor, la ejecución sigue con la declaración después de NEXT declaración. Este es FOR-NEXT lazo.

El cuerpo del lazo es saltado si el valor inicial de los tiempos de lazo el signo del paso excede los tiempos de valor finales el signo del paso.

Si STEP es negativo, se pone que el valor final del contador sea menos que el valor inicial. El contador es decrementado cada vez por el lazo, y el lazo es ejecutado hasta que el contador sea menos que el valor final.

Lazos anidados:

FOR-NEXT los lazos pueden ser anidados; es decir FOR-NEXT el lazo puede ser colocado dentro del contexto del otro FOR-NEXT lazo. Cuando los lazos son anidados, cada lazo debe tener un nombre variable único como su contador.

NEXT declaración para el lazo interior debe aparecer antes de esto para el lazo exterior.

Si anidó los lazos tienen el mismo punto de final, NEXT declaración sola puede ser usada para todos ellos.

La variable(s) en la instruccion NEXT  puede ser omitida, en cuyo caso declaración NEXT  emparejará el más reciente FOR la declaración. ?

Si se encuentra declaración NEXT  antes de que su correspondencia FOR la declaración, el mensaje de error "NEXT without FOR"  sea publicada y la ejecución es terminada.

Ejemplos:

El ejemplo siguiente imprime valores de número entero de la variable I % de 1 a 10 en pasos de z. Para la ejecución más rápida, I es declarado como un número entero por el signo %.

10 K=10
20 FOR I%=1 TO K STEP 2
30   PRINT I%
40 NEXT
RUN
 1
 3
 5
 7
 9

En el ejemplo siguiente, el lazo no ejecuta porque el valor inicial del lazo excede el valor final. Nada es imprimido por este ejemplo.

10 R=0
20 FOR S=1 TO R
30   PRINT S
40 NEXT S

En el siguiente ejemplo, el lazo ejecuta 10 veces. El valor final para la variable de lazo siempre es puesto antes de que el valor inicial sea puesto.

10 S=5
20 FOR S=1 TO S+5
30   PRINT S;
40 NEXT
RUN
 1 2 3 4 5 6 7 8 9 10
 

FOR ... NEXT Statements

Purpose:

To execute a series of instructions a specified number of times in a loop.

Syntax:

FOR variable=x TO y [STEP z] 
. 
. 
. 
NEXT [variable][,variable...]

Comments:

variable is used as a counter.

x,y, and z are numeric expressions.

STEP z specifies the counter increment for each loop.

The first numeric expression (x) is the initial value of the counter. The second numeric expression (y) is the final value of the counter.

Program lines following the FOR statement are executed until the NEXT statement is encountered. Then, the counter is incremented by the amount specified by STEP.

If STEP is not specified, the increment is assumed to be 1.

A check is performed to see if the value of the counter is now greater than the final value (y). If it is not greater, GW-BASIC branches back to the statement after the FOR statement, and the process is repeated. If it is greater, execution continues with the statement following the NEXT statement. This is a FOR-NEXT loop.

The body of the loop is skipped if the initial value of the loop times the sign of the step exceeds the final value times the sign of the step.

If STEP is negative, the final value of the counter is set to be less than the initial value. The counter is decremented each time through the loop, and the loop is executed until the counter is less than the final value.

Nested Loops:

FOR-NEXT loops may be nested; that is, a FOR-NEXT loop may be placed within the context of another FOR-NEXT loop. When loops are nested, each loop must have a unique variable name as its counter.

The NEXT statement for the inside loop must appear before that for the outside loop.

If nested loops have the same end point, a single NEXT statement may be used for all of them.

The variable(s) in the NEXT statement may be omitted, in which case the NEXT statement will match the most recent FOR statement.

If a NEXT statement is encountered before its corresponding FOR statement, a "NEXT without FOR" error message is issued and execution is terminated.

Examples:

The following example prints integer values of the variable I% from 1 to 10 in steps of z. For fastest execution, I is declared as an integer by the % sign.

10 K=10
20 FOR I%=1 TO K STEP 2
30   PRINT I%
40 NEXT
RUN
 1
 3
 5
 7
 9

In the following example, the loop does not execute because the initial value of the loop exceeds the final value. Nothing is printed by this example.

10 R=0
20 FOR S=1 TO R
30   PRINT S
40 NEXT S

In the next example, the loop executes 10 times. The final value for the loop variable is always set before the initial value is set.

10 S=5
20 FOR S=1 TO S+5
30   PRINT S;
40 NEXT
RUN
 1 2 3 4 5 6 7 8 9 10
1