Contenuti del Corso
Java Basics
Java Basics
While-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
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
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.
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:
Main
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.
Swipe to start coding
Find the sum of a range of numbers from 1 to n
.
- Create a method called
sumFrom1ToN(int n)
that returns anint
result. - Inside the method, define two variables:
sum
(to store the total).current
(starting from 1).
- Use a
while
loop to repeat the following steps untilcurrent
is greater thann
:- Add the value of
current
to thesum
. - Increment
current
by1
.
- Add the value of
- Return the total
sum
at the end of the loop.
Soluzione
solution
Grazie per i tuoi commenti!
While-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
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
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.
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:
Main
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.
Swipe to start coding
Find the sum of a range of numbers from 1 to n
.
- Create a method called
sumFrom1ToN(int n)
that returns anint
result. - Inside the method, define two variables:
sum
(to store the total).current
(starting from 1).
- Use a
while
loop to repeat the following steps untilcurrent
is greater thann
:- Add the value of
current
to thesum
. - Increment
current
by1
.
- Add the value of
- Return the total
sum
at the end of the loop.
Soluzione
solution
Grazie per i tuoi commenti!