Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn While-Loop | Loops
Java Basics

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.

Task

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

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 2
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

close

bookWhile-Loop

Swipe to show 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.

Task

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 desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 2
single

single

some-alt