Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara 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.

Compito

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.

Soluzione

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 16
single

single

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

close

bookWhile-Loop

Scorri per mostrare il 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.

Compito

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.

Soluzione

Switch to desktopCambia al desktop per esercitarti nel mondo realeContinua da dove ti trovi utilizzando una delle opzioni seguenti
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 16
single

single

some-alt