In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "Java loop sentences for, while and the usage difference of do … while". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Java loop structure-for, while and do...while
Program statements with sequential structures can only be executed once. If you want the same operation to be performed multiple times, you need to use a loop structure.
There are three main loop structures in Java:
1. While cycle
2. Do... While cycle
3. For cycle
An enhanced for loop, which is mainly used in arrays, is introduced in Java5.
. . .
While cycle
While is the most basic loop, which has the following structure:
While (Boolean expression) {
/ / Loop content
}
As long as the Boolean expression is true, the loop executes forever.
. . .
Do... While cycle http://www.iis7.com/a/lm/yczmljgj/
For while statements, you cannot enter a loop if the condition is not met. But sometimes we need to execute at least once, even if the conditions are not met.
Do... The while loop is similar to the while loop, except that do... The while loop is executed at least once.
Do {
/ / Code statement
} while (Boolean expression)
Note: the Boolean expression is after the body of the loop, so the statement block is executed before the Boolean expression is detected. If the Boolean expression evaluates to true, the statement block executes until the Boolean expression evaluates to false.
. . .
For cycle
Although all loop structures can be represented in while or do...while, Java provides another statement-- for loops, which makes some loop structures simpler.
The number of times the for loop is executed is determined before execution. The syntax format is as follows:
For (initialization; Boolean expression; update) {
/ / Code statement
}
There are the following notes about the for loop:
-the initialization step is performed first. You can declare a type, but you can initialize one or more loop control variables, or it can be an empty statement.
-then, detect the value of the Boolean expression. If true, the loop body is executed. If false, the loop terminates and the statement following the body of the loop begins to execute.
-after performing a loop, update the loop control variable.
-detect the Boolean expression again. Loop through the above process.
. . .
Java enhanced for cycle
Java5 introduces an enhanced for loop that is mainly used in arrays.
The syntax format of the Java enhanced for loop is as follows:
For (declaration statement: expression)
{
/ / Code sentence
}
Declaration statement: declare a new local variable whose type must match the type of the array element. Its scope is limited to the looping statement block, and its value is equal to the value of the array element at this time.
Expression: an expression is the name of the array to access, or a method that returns an array.
. . .
Break keyword
Break is mainly used in loop statements or switch statements to jump out of the whole statement block.
Break jumps out of the innermost loop and continues to execute the statements below the loop.
Syntax: the use of break is very simple, which is a statement in the loop structure: break
. . .
Continue keyword
Continue is suitable for any loop control structure. The function is to make the program jump to the next iteration of the loop immediately.
In the for loop, the continue statement causes the program to jump to the update statement immediately.
In while or do... In the while loop, the program immediately jumps to the judgment statement of the Boolean expression.
Syntax: continue is a simple statement in the body of the loop: continue
This is the end of the introduction of "Java loop sentences for, while and do … while usage differences". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.