while Loops · Bucles while
| English | Español |
|---|---|
| while loop/waɪl luːp/ | bucle while |
| loop control variable/luːp kənˈtrəʊl ˈveərɪəbl/ | variable de control del bucle |
| infinite loop/ˈɪnfɪnət luːp/ | bucle infinito |
Repeat while true
- A while loop 当型循环 repeats its body as long as a condition stays
true. while (i <= 5) { sum = sum + i; i++; }- Before each pass, Java checks the condition; if
false, the loop ends. - Use
whilewhen you don't know in advance how many passes you'll need.
Repetir mientras sea verdad
- Un bucle while (ciclo de tipo cuando) repite su cuerpo mientras una condición permanezca
true. while (i <= 5) { sum = sum + i; i++; }- Antes de cada iteración, Java verifica la condición; si es
false, el bucle termina. - Utilice
whilecuando no sepa de antemano cuántas iteraciones necesitará.
Test-before-each-pass
- The condition is tested before the body runs each time.
- If it's
falseon the very first check, the body runs zero times. - After each pass, the condition is re-checked before the next.
- This "check, then maybe run" order is worth memorizing.
Prueba antes de cada paso
- La condición se verifica antes de que el cuerpo se ejecute cada vez.
- Si es
falseen la primera verificación, el cuerpo se ejecuta cero veces. - Después de cada iteración, la condición se vuelve a verificar antes de la siguiente.
- Este orden de "verificar, y luego quizás ejecutar" vale la pena memorizarlo.
The loop control variable
- A loop control variable 循环控制变量 drives progress toward stopping.
- Initialize it before the loop, use it in the condition, and change it inside the body.
istarts at1, the condition checksi <= 5, andi++moves it forward.- Without a change that eventually breaks the condition, the loop never ends.
La variable de control del bucle
- Una variable de control del bucle 循环控制变量 impulsa el progreso hacia la detención.
- Inicialícela antes del bucle, utilízela en la condición y cámbiela dentro del cuerpo.
icomienza en1, la condición verificai <= 5, yi++la avanza.- Sin un cambio que eventualmente rompa la condición, el bucle nunca termina.
Infinite loops
- An infinite loop 无限循环 happens when the condition never becomes false.
- Forgetting
i++(or updating the wrong variable) traps the program forever. - The tell: the loop makes no progress toward its stopping condition.
- Always make sure each pass moves the control variable toward the exit.
Bucles infinitos
- Un bucle infinito 无限循环 ocurre cuando la condición nunca se vuelve falsa.
- Olvidar
i++(o actualizar la variable incorrecta) atrapa al programa para siempre. - La señal: el bucle no hace ningún progreso hacia su condición de parada.
- Asegúrese siempre de que cada paso mueva la variable de control hacia la salida.
A while checks its condition BEFORE each pass — so it can run zero times. If the condition is already false, the body never runs. And you must change the loop control variable inside the body so the condition eventually turns false; forgetting the update (e.g. no i++) is the classic infinite loop.
Un while verifica su condición ANTES de cada paso — por lo que puede ejecutarse cero veces. Si la condición ya está false, el cuerpo nunca se ejecuta. Y debe cambiar la variable de control del bucle dentro del cuerpo para que la condición finalmente se convierta en false; olvidar la actualización (por ejemplo, sin i++) es el clásico bucle infinito.
Summing 1 to 5:
int i = 1, sum = 0;while (i <= 5) { sum = sum + i; i++; }- Passes:
i=1sum=1,i=2sum=3,i=3sum=6,i=4sum=10,i=5sum=15; theni=6fails<= 5→ stop.sumis15.
Sumando del 1 al 5:
int i = 1, sum = 0;while (i <= 5) { sum = sum + i; i++; }- Pasos:
i=1sum=1,i=2sum=3,i=3sum=6,i=4sum=10,i=5sum=15; luegoi=6falla<= 5→ detener.sumes15.
A while loop repeats its body while a condition is true, checking before each pass (so it may run zero times). A loop control variable must be initialized, tested, and updated inside the body so the loop makes progress — forgetting the update causes an infinite loop.
Un bucle while repite su cuerpo mientras una condición sea true, verificando antes de cada paso (por lo que puede ejecutarse cero veces). Una variable de control del bucle debe inicializarse, verificarse y actualizarse dentro del cuerpo para que el bucle haga progreso; olvidar la actualización causa un bucle infinito.
Summing 1 to 5 with while · Sumar del 1 al 5 con while
Each pass adds i to sum, then i++ moves toward the exit. · Cada pasada añade i a sum, luego i++ avanza hacia la salida.
A while loop checks its condition... · Un bucle while verifica su condición...
The condition is tested before each pass, so it can run 0 times. · La condición se prueba antes de cada pasada, por lo que puede ejecutarse 0 veces.
int i=1, sum=0; while (i<=5){ sum+=i; i++; } What is sum at the end? · int i=1, sum=0; while (i<=5){ sum+=i; i++; } ¿Cuál es el valor de sum al final?
1+2+3+4+5 = 15.
Forgetting to update the loop control variable inside the body usually causes... · Olvidar actualizar la variable de control del bucle dentro del cuerpo generalmente causa...
With no progress, the condition never becomes false. · Sin progreso, la condición nunca se vuelve falsa.
If a while condition is false on the first check, the body runs zero times. · Si una condición while es falsa en la primera comprobación, el cuerpo se ejecuta cero veces.
Test-before-each-pass means it can run 0 times. · Prueba antes de cada pasada significa que puede ejecutarse 0 veces.
A loop that never stops because its condition never becomes false is an ___ loop (one word). · Un bucle que nunca se detiene porque su condición nunca se vuelve falsa es un bucle ___ (una palabra).
An infinite loop makes no progress toward stopping. · Un bucle infinito no avanza hacia la detención.