Repetir con while
Repita hasta que cambie una condición.
Repetir con while es una lección gratuita de Mojo Academy en CoddyKit. Esta es la lección 2 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Mojo Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Mojo Academy incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
Repeat Until Done
Sometimes you must run code again and again. A while loop repeats its block as long as a condition stays true. 🔁
The while Shape
Write while, a condition, a colon, then an indented block. Mojo rechecks the condition before every pass through the body.
while count < 3:
print(count)Change the Condition Inside
The body must eventually make the condition false. Usually you update a counter so the loop progresses toward stopping.
var count = 0
while count < 3:
print(count)
count += 1Watch for Infinite Loops
If the condition never turns false, the loop runs forever. An infinite loop usually means you forgot to update the variable it checks.
Condition Checked First
Mojo tests the condition before the first pass. If it is already false, the body never runs even once.
while False:
print("never shown")Accumulate a Result
Loops shine when building up a value. Keep a running total in a variable and add to it on each pass.
var total = 0
var i = 1
while i <= 5:
total += i
i += 1Counting Down
A while loop can move in any direction. Start high and decrement each pass to count down toward a stopping point.
var n = 3
while n > 0:
print(n)
n -= 1Loop on a Flag
The condition need not be a number. A boolean flag works too, letting other logic decide when the loop should end.
var running = True
while running:
running = step()Combine with if
Inside a while body you can branch with if. This lets each pass make its own decision based on the current state.
while i < 10:
if i % 2 == 0:
print(i)
i += 1Pick while When Count Is Unknown
Use while when you do not know in advance how many passes you need, such as reading until a value appears.
Update Before You Forget
A reliable habit: put the update step right where the loop must make progress, so the condition can truly change each pass.
Quick Check
You wrote a while loop but it runs forever and never stops. What is the most likely cause?
Recap
A while loop repeats while its condition is true, checking before each pass. Update the condition inside the body to avoid looping forever. 🎯
Preguntas frecuentes
¿La lección «Repetir con while» es gratis?
Sí — el texto completo de «Repetir con while» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Mojo Academy, actualiza a CoddyKit PRO. El curso de Mojo Academy incluye 4 lecciones en total.
¿Qué aprenderé en «Repetir con while»?
Repita hasta que cambie una condición. Practicas Mojo Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar Mojo Academy?
No se requiere experiencia previa. Mojo Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 2 de 4.
¿Cuánto tiempo toma la lección «Repetir con while»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de Mojo Academy?
Sí. Cada lección de Mojo Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Tomar decisiones con if/else
- Repetir con while
- Iterar con for y range
- break, continue y salida anticipada