Iteration: FOR · Iteración: FOR
FOR repeats a fixed number of times
- A
FORloop counts from a start value to an end value. NEXTmarks the end of the loop body.
FOR I ← 1 TO 5
OUTPUT I // 1, 2, 3, 4, 5
NEXT I
A running total
- Start a total at 0, then add to it inside the loop.
Total ← 0
FOR I ← 1 TO 10
Total ← Total + I
NEXT I
OUTPUT Total // → 55
Trace the running total · Rastrear la suma acumulada
A FOR loop repeats a set number of times. Step through and watch Total grow. · Un bucle FOR se repite un número determinado de veces. Avanza paso a paso y observa cómo crece Total.
STEP changes the count
STEPsets how much the counter changes each pass.STEP -1counts down.
FOR I ← 5 TO 1 STEP -1
OUTPUT I // 5, 4, 3, 2, 1
NEXT I
Watch out · Precaución
- Use
FORwhen you know how many times to repeat; useWHILEwhen you do not. - The counter (
I) andNEXT Imust use the same · mismo name.
Common mistakes
FOR I ← 1 TO N ... NEXT Irunsntimes, including both ends.- The counter changes automatically each turn.
Now you try
- Use
FOR ... NEXT. Type<-for←. - Press Check answer to test it.
Use a FOR loop to output the numbers 1 to · hasta 5, one per line. · Usa un bucle FOR para imprimir los números del 1 al 5, uno por línea.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Use a FOR loop to add up 1 to · hasta 10, then output the total (it is 55). · Usa un bucle FOR para sumar del 1 al 10, luego imprime el total (es 55).
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Read an INTEGER N, then output N × 1, N × 2, N × 3 (one per line). For input 4, output 4, 8, 12. · Lee un INTEGER N, luego imprime N × 1, N × 2, N × 3 (uno por línea). Para entrada 4, imprime 4, 8, 12.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.