Home > Java > javaTutorial > How to implement while loop in Java

How to implement while loop in Java

王林
Release: 2023-04-22 11:28:19
forward
2603 people have browsed it

1. Loop structure statement

1. In our real life, we often repeat the same thing several times. For example: Chaoshan people like to drink tea, as long as they are in the process of drinking tea, the act of brewing; the ups and downs of doing push-ups, etc. There is a special statement in Java called a loop statement, which can execute a piece of code repeatedly. For example, find the sum of integers from 1 to 10.

2. Loop statements are divided into three types: while loop statement, do-while loop statement, and for loop statement.

2. While loop statement

1. The while loop statement is an execution statement that determines whether to execute the curly brackets ({}) based on conditional judgment. The while statement will repeatedly perform conditional judgment. As long as the condition is true, the statement in curly brackets ({}) will be executed until the condition is not true and the while loop ends.

2. The syntax statement of while loop statement

[初始化语句] while( 条件 ){ s1:执行语句 }
Copy after login

In the syntax structure, the execution statement in {} is called the loop body. Whether the loop body is executed depends on whether the conditions are met.

3.Flow chart of while loop

How to implement while loop in Java

#When the condition is true, the loop body will be executed. After the loop body is executed, the condition will continue to be judged. Whether the loop condition is true, if it is true, continue to execute the loop body until the loop condition is false, and the while loop ends.

4. Use the while loop statement to find the sum of integers from 1 to 10

int i = 1, sum = 0;  while(i <p>System.out.println("The sum of integers from 1 to 10 is " sum);</p><p>The output result is: </p><pre class="brush:php;toolbar:false">1-10的整数和值为55
Copy after login

First, define int type variables i and sum, i=1, sum=0 are used to save the results of calculating the sum of integers from 1 to 10. From i equals 1, as long as i is less than or equal to 10, the statement of the loop body will be executed. sum =i is equivalent to sum=sum i. The value of i increases until the condition is not met. The while loop ends and outputs the sum of integers from 1 to 10. the result of.

3. do-while loop statement

1. The do-while loop statement is similar to the while loop, and the syntax statement

do{ s1:执行语句 } while( 条件 );
Copy after login

is in the above syntax In the statement, the curly brace ({}) execution statement after do is the loop body, and the condition of the do-while loop is placed after the loop body. This means that the loop body is executed first, and then the condition is judged. If the condition is met, the loop body continues to be executed. Otherwise, the loop is exited and the statements following the do-while loop are executed. Therefore, in a do-while loop, the loop body is executed one or several times.

2. Do-while loop statement flow chart

How to implement while loop in Java

3. Use do-while loop to find the sum of integers from 1 to 10

int i = 1, sum = 0; do{ sum += i; i++; }while(i <p>The output result is: </p><pre class="brush:php;toolbar:false">1-10的整数和值为55
Copy after login

First define the int type variables i and sum, i=1, sum is used to save the value of the integer sum of 1-10, execute the loop body first, sum =i is equivalent sum=sum the value of i, the value of sum is 1, i increments, and then checks whether the condition of i

4. Use of break

1. In a loop statement, use the break statement to jump out of the loop directly, ignoring any other statements in the loop body and loop condition tests. In other words, when a break statement is encountered in a loop, the loop terminates and the program continues execution at the statement following the loop.

2.break usage example is as follows:

int i=0; while(i<p>The output result is: 1234</p><p>You can see that the int type variable i is defined as 0, because i=0, i</p>
Copy after login

The above is the detailed content of How to implement while loop in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template