Appearance
While Loop
The loop statement allows the computer to perform loop calculations based on conditions, continue the loop when the conditions are met, and exit the loop when the conditions are not met.
For example, to calculate the sum from 1 to 100:
1 + 2 + 3 + 4 + … + 100 = ?
n addition to using sequence formulas, you can let the computer do 100 loop accumulations. Because computers are characterized by very fast calculations, it takes less than 1 second to make the computer cycle 100 million times. Therefore, many calculation tasks cannot be calculated by humans, but computers can calculate them using a simple and crude method such as loops. Results can be obtained quickly.
Let’s first look at the while
conditional loop provided by Java. Its basic usage is:
java
while (conditional-expression) {
Loop statement
}
// Continue to execute subsequent code
The while
loop first determines whether the condition is true
before each loop starts. If the calculation result is true
, execute the statements in the loop body once. If the calculation result is false
, jump directly to the end of the while
loop and continue execution.
We use a while loop to accumulate from 1 to 100, which can be written like this:
java
public class Main {
public static void main(String[] args) {
int sum = 0; // Accumulated sum, initialized to 0
int n = 1;
while (n <= 100) { // The loop condition is n <= 100
sum = sum + n; // Add n to sum
n ++; // n adds 1 to itself
}
System.out.println(sum); // 5050
}
}
Note that the while
loop first determines the loop condition and then loops. Therefore, it is possible to not perform a loop at all.
For the judgment of loop conditions and the processing of auto-increasing variables, special attention should be paid to boundary conditions. Consider why the following code does not get the correct results:
java
public class Main {
public static void main(String[] args) {
int sum = 0;
int n = 0;
while (n <= 100) {
n ++;
sum = sum + n;
}
System.out.println(sum);
}
}
If the loop condition is always satisfied, the loop becomes an infinite loop. An infinite loop will cause 100% CPU usage, and users will feel that the computer is running slowly, so avoid writing infinite loop code.
If there is a problem with the logic of the loop condition, it will also cause unexpected results:
java
public class Main {
public static void main(String[] args) {
int sum = 0;
int n = 1;
while (n > 0) {
sum = sum + n;
n ++;
}
System.out.println(n); // -2147483648
System.out.println(sum);
}
}
On the surface, the above while
loop is an infinite loop. However, Java's int
type has a maximum value. After reaching the maximum value, adding 1 will turn it into a negative number. As a result, the while
loop is unexpectedly exited.
Practise
Use while
to calculate the sum from m
to n
:
java
public class Main {
public static void main(String[] args) {
int sum = 0;
int m = 20;
int n = 100;
// Use while to calculate M+...+N:
while (false) {
}
System.out.println(sum);
}
}
Summary
while
loop first determines whether the loop condition is met, and then executes the loop statement;
The while
loop may not be executed even once;
When writing loops, pay attention to loop conditions and avoid infinite loops.