Iteration — loops · Iteración — bucles
Count-controlled loops
- Iteration means repeating steps.
- A count-controlled loop repeats a fixed number of times.
- In Python that is
for i in range(n):.
Bucle controlado por contador
- Iteración significa repetir pasos.
- Un bucle controlado por contador se repite un número fijo de veces.
- En Python, esto se escribe como
for i in range(n):.
for i in range(5):
print("Hello")
Choosing the range
range(5)gives0, 1, 2, 3, 4.range(1, 6)gives1, 2, 3, 4, 5— it stops before 6.range(2, 11, 2)counts in steps of 2.
Elegir el rango
range(5)produce0, 1, 2, 3, 4.range(1, 6)produce1, 2, 3, 4, 5— se detiene antes de llegar a 6.range(2, 11, 2)cuenta en incrementos de 2.
for n in range(1, 6):
print(n)
Pre-condition loops: while
- A pre-condition loop tests before each pass.
- It may run zero times if the test is false at the start.
Bucle de pre-condición: while
- Un bucle de pre-condición verifica la condición antes de cada iteración.
- Puede ejecutarse cero veces si la condición es falsa desde el inicio.
count = 0
while count < 5:
print(count)
count = count + 1
Post-condition loops: REPEAT UNTIL
- A post-condition loop tests after each pass, so it always runs at least once.
- Python has no
repeat, so we usewhile Truewithbreak.
Bucle de post-condición: REPEAT UNTIL
- Un bucle de post-condición verifica la condición después de cada iteración, por lo que siempre se ejecuta al menos una vez.
- Python no tiene
repeat, así que usamoswhile Trueconbreak.
total = 0
while True:
total = total + 1
if total >= 3:
break
print(total)
Each pass changes count · Cada paso cambia el contador
A loop repeats its body; count must change so the test finally fails. · Un bucle repite su cuerpo; count debe cambiar para que la prueba finalmente falle.
In the exam: FOR, WHILE, REPEAT
- Pseudocode has all three loop types.
En el examen: FOR, WHILE, REPEAT
- El pseudocódigo incluye los tres tipos de bucles.
FOR I ← 1 TO 5
OUTPUT I
NEXT I
Common mistakes
- A
forloop repeats a known number of times; awhilerepeats until something changes. range(n)runs0ton - 1.- Change the condition inside a
while, or it never ends.
Errores comunes
- Un bucle
forse repite un número conocido de veces; unwhilese repite hasta que algo cambie. range(n)recorre desde0hastan - 1.- Cambia la condición dentro de un
while, o nunca terminará.
Now you try
- Pick the loop that fits each task.
- Press Check answer to test your code.
Ahora practica
- Selecciona el bucle adecuado para cada tarea.
- Pulsa Comprobar respuesta para verificar tu código.
Use a for loop to print the numbers 1 to · hasta 5, one on each line. · Use un bucle for para imprimir los números 1 a 5, uno en cada línea.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Use a loop to add up the numbers 1 to · hasta 10, then print the total (it is 55). · Use un bucle para sumar los números 1 a 10, luego imprima el total (es 55).
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Read a whole number n, then count down · hacia abajo from n to · hasta 1, one per line. For input 3, print 3, 2, 1. · Lea un número entero n, luego cuente hacia abajo desde n hasta 1, uno por línea. Para la entrada 3, imprima 3, 2, 1.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.