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

While-LoopWhile-Loop

In the previous chapter, you could see that with the help of a loop, we repeated the code 10 times. Now, let's examine the syntax of one of these loops.

While-Loop Syntax

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.

java

Main.java

To remember how this loop works, you can follow a simple rule: While the condition is true, perform the operation. For example, while it's raining, I use an umbrella. As soon as the rain stops, I go without an umbrella.

It's raining - the condition
I use an umbrella - the code executed inside the loop
The rain has stopped - the compiler exits the loop and stops executing the code inside the loop.

It's that simple.

Now, let's take a look at a more detailed explanation of how the while loop works:

  • The condition is evaluated before each iteration. If the condition is true, the code block inside the loop is executed. If the condition is false, the loop is terminated, and the program continues with the next statement after the loop;
  • The code block inside the loop can contain one or more statements. These statements will be executed repeatedly as long as the condition remains true;
  • It's important to ensure that the condition eventually becomes false, or the loop will run indefinitely, causing an infinite loop.

Here's an example to demonstrate the while loop:

java

Main.java

In this code, we have two variables of type int. 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.

Note

The while loop is useful when the number of iterations is not known beforehand and depends on a specific condition. It allows you to repeat a code block until the condition is no longer satisfied.

Task

Print the numbers from 1 to 5 using a while loop.

Everything was clear?

Section 3. Chapter 2
toggle bottom row
course content

Course Content

Java Basics

While-LoopWhile-Loop

In the previous chapter, you could see that with the help of a loop, we repeated the code 10 times. Now, let's examine the syntax of one of these loops.

While-Loop Syntax

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.

java

Main.java

To remember how this loop works, you can follow a simple rule: While the condition is true, perform the operation. For example, while it's raining, I use an umbrella. As soon as the rain stops, I go without an umbrella.

It's raining - the condition
I use an umbrella - the code executed inside the loop
The rain has stopped - the compiler exits the loop and stops executing the code inside the loop.

It's that simple.

Now, let's take a look at a more detailed explanation of how the while loop works:

  • The condition is evaluated before each iteration. If the condition is true, the code block inside the loop is executed. If the condition is false, the loop is terminated, and the program continues with the next statement after the loop;
  • The code block inside the loop can contain one or more statements. These statements will be executed repeatedly as long as the condition remains true;
  • It's important to ensure that the condition eventually becomes false, or the loop will run indefinitely, causing an infinite loop.

Here's an example to demonstrate the while loop:

java

Main.java

In this code, we have two variables of type int. 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.

Note

The while loop is useful when the number of iterations is not known beforehand and depends on a specific condition. It allows you to repeat a code block until the condition is no longer satisfied.

Task

Print the numbers from 1 to 5 using a while loop.

Everything was clear?

Section 3. Chapter 2
toggle bottom row
some-alt