Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære While-Loop | Section
Practice
Projects
Quizzes & Challenges
Quizer
Challenges
/
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.

Oppgave

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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 16
single

single

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

close

bookWhile-Loop

Sveip for å vise menyen

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.

Oppgave

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 desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 16
single

single

some-alt