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:执行语句 }
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
#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
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( 条件 );
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
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
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>
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!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment