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

Course Content

Java Basics

Java Basics

1. Getting Started
2. Basic Types, Operations
3. Loops
4. Arrays
5. String

bookWhile-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

copy
123
while (condition) { // code to be executed }

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

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 equals to b: " + (a == b)); } }

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.

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
toggle bottom row

bookWhile-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

copy
123
while (condition) { // code to be executed }

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

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 equals to b: " + (a == b)); } }

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.

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
toggle bottom row

bookWhile-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

copy
123
while (condition) { // code to be executed }

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

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 equals to b: " + (a == b)); } }

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.

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!

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

copy
123
while (condition) { // code to be executed }

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

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 equals to b: " + (a == b)); } }

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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 3. Chapter 2
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt