Contenido del Curso
Principios Básicos de Java
Principios Básicos de Java
Loop While
In the previous chapter, you could see that with the help of a loop, we repeated the code 10 times. Now, let's examine the syntax of one of these loops.
En el capítulo anterior, pudiste ver que con la ayuda de un loop, repetimos el código 10 veces. Ahora, vamos a examinar la sintaxis de uno de estos loops.
Main
while (condition) { // code to be executed }
To remember how this loop works, you can follow a simple rule: While the condition is true, perform the operation. For example, while it's raining, I use an umbrella. As soon as the rain stops, I go without an umbrella.
It's raining - the condition
I use an umbrella - the code executed inside the loop
The rain has stopped - the compiler exits the loop and stops executing the code inside the loop.
It's that simple.
Para recordar cómo funciona este loop, puedes seguir una regla sencilla: Mientras la condición es true, realiza la operación. Por ejemplo, mientras llueve, uso paraguas. En cuanto deja de llover, me voy sin paraguas.
Está lloviendo - la condition
Uso un paraguas - el código ejecutado dentro del loop
Ha dejado de llover - el compilador sale del loop y deja de ejecutar el código dentro del loop.
Es así de sencillo.
Here's an example to demonstrate the while
loop:
Main
package com.example; public class Main { public static void main(String[] args) { int a = 0; int b = 10; while (a != b) { a = a + 1; System.out.println("a has value: " + a); b = b - 1; System.out.println("b has value: " + b); } System.out.println("Is a equals to b: " + (a == b)); } }
He aquí un ejemplo para demostrar el loop while
:
Swipe to start coding
Print the numbers from 1 to 5 using a while
loop.
Solución
solution
¡Gracias por tus comentarios!
Loop While
In the previous chapter, you could see that with the help of a loop, we repeated the code 10 times. Now, let's examine the syntax of one of these loops.
En el capítulo anterior, pudiste ver que con la ayuda de un loop, repetimos el código 10 veces. Ahora, vamos a examinar la sintaxis de uno de estos loops.
Main
while (condition) { // code to be executed }
To remember how this loop works, you can follow a simple rule: While the condition is true, perform the operation. For example, while it's raining, I use an umbrella. As soon as the rain stops, I go without an umbrella.
It's raining - the condition
I use an umbrella - the code executed inside the loop
The rain has stopped - the compiler exits the loop and stops executing the code inside the loop.
It's that simple.
Para recordar cómo funciona este loop, puedes seguir una regla sencilla: Mientras la condición es true, realiza la operación. Por ejemplo, mientras llueve, uso paraguas. En cuanto deja de llover, me voy sin paraguas.
Está lloviendo - la condition
Uso un paraguas - el código ejecutado dentro del loop
Ha dejado de llover - el compilador sale del loop y deja de ejecutar el código dentro del loop.
Es así de sencillo.
Here's an example to demonstrate the while
loop:
Main
package com.example; public class Main { public static void main(String[] args) { int a = 0; int b = 10; while (a != b) { a = a + 1; System.out.println("a has value: " + a); b = b - 1; System.out.println("b has value: " + b); } System.out.println("Is a equals to b: " + (a == b)); } }
He aquí un ejemplo para demostrar el loop while
:
Swipe to start coding
Print the numbers from 1 to 5 using a while
loop.
Solución
solution
¡Gracias por tus comentarios!