Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre While-Loop | Section
Java Fundamentals

bookWhile-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

Main.java

copy
123
while (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

Main.java

copy
123456789101112131415
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 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.

Tâche

Swipe to start coding

Find the sum of a range of numbers from 1 to n.

  1. Create a method called sumFrom1ToN(int n) that returns an int result.
  2. Inside the method, define two variables:
    • sum (to store the total).
    • current (starting from 1).
  3. Use a while loop to repeat the following steps until current is greater than n:
    • Add the value of current to the sum.
    • Increment current by 1.
  4. Return the total sum at the end of the loop.

Solution

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 16
single

single

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

close

bookWhile-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

Main.java

copy
123
while (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

Main.java

copy
123456789101112131415
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 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.

Tâche

Swipe to start coding

Find the sum of a range of numbers from 1 to n.

  1. Create a method called sumFrom1ToN(int n) that returns an int result.
  2. Inside the method, define two variables:
    • sum (to store the total).
    • current (starting from 1).
  3. Use a while loop to repeat the following steps until current is greater than n:
    • Add the value of current to the sum.
    • Increment current by 1.
  4. Return the total sum at the end of the loop.

Solution

Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 16
single

single

some-alt