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

Opgave

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.

Løsning

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 16
single

single

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

close

bookWhile-Loop

Stryg for at vise menuen

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.

Opgave

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.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 16
single

single

some-alt