While-Loop
while Loop
The while loop is the simplest example of a loop in action. This loop will repeatedly execute a code block as long as the condition inside the condition block evaluates to true. Once the condition returns false, the loop execution will stop.
Main.java
123while (condition) { // code to be executed }
The while loop works as follows:
- The condition is checked before each iteration; if true, the loop runs, if false, it ends;
- The loop can contain one or more statements, executed repeatedly while the condition is true;
- Ensure the condition eventually becomes false to avoid an infinite loop.
Here's an example to demonstrate the while loop:
Main.java
123456789101112131415package 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 equal to b: " + (a == b)); } }
In our loop, we set a condition that reads as follows: as long as the value of variable a is not equal to the value of variable b, we increment variable a and decrement variable b. When their values are equal, we terminate the while loop.
Swipe to start coding
Find the sum of a range of numbers from 1 to n.
- Create a method called
sumFrom1ToN(int n)that returns anintresult. - Inside the method, define two variables:
sum(to store the total).current(starting from 1).
- Use a
whileloop to repeat the following steps untilcurrentis greater thann:- Add the value of
currentto thesum. - Increment
currentby1.
- Add the value of
- Return the total
sumat the end of the loop.
Solution
Merci pour vos commentaires !
single
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Génial!
Completion taux amélioré à 2.86
While-Loop
Glissez pour afficher le menu
while Loop
The while loop is the simplest example of a loop in action. This loop will repeatedly execute a code block as long as the condition inside the condition block evaluates to true. Once the condition returns false, the loop execution will stop.
Main.java
123while (condition) { // code to be executed }
The while loop works as follows:
- The condition is checked before each iteration; if true, the loop runs, if false, it ends;
- The loop can contain one or more statements, executed repeatedly while the condition is true;
- Ensure the condition eventually becomes false to avoid an infinite loop.
Here's an example to demonstrate the while loop:
Main.java
123456789101112131415package 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 equal to b: " + (a == b)); } }
In our loop, we set a condition that reads as follows: as long as the value of variable a is not equal to the value of variable b, we increment variable a and decrement variable b. When their values are equal, we terminate the while loop.
Swipe to start coding
Find the sum of a range of numbers from 1 to n.
- Create a method called
sumFrom1ToN(int n)that returns anintresult. - Inside the method, define two variables:
sum(to store the total).current(starting from 1).
- Use a
whileloop to repeat the following steps untilcurrentis greater thann:- Add the value of
currentto thesum. - Increment
currentby1.
- Add the value of
- Return the total
sumat the end of the loop.
Solution
Merci pour vos commentaires !
single