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/02 Report--
This article is about examples of the use of loop structures in Java. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
The basic structure of Java programming-Loop structure
Text keywords: basic structure, loop structure, while, do-while, for
I. the basic structure of the cycle
For convenience, we can divide the loop structure into four parts to describe. Of course, these four parts do not have to appear, and even when they do not appear, they can form a circular structure. Moreover, for different loop structures, the relative position of each part is also different.
1. Initialization section
Before entering the loop structure, you can define some variables that are used in the loop and after the end of the loop, and assign values.
two。 Judgment condition part
Used to determine whether to continue the loop, and if it is false, the loop ends. The judgment part can directly use a Boolean variable or an expression, but you must ensure that the result of the expression is of Boolean type.
3. Circulatory body part
The core part of the loop structure, used to write code that you want to repeat each time, usually designed according to the function of the program.
4. Iterative part
Used to assist in controlling the end of the loop, executed after the body of the loop, such as variable self-increment, etc.
Similar to the judgment structure, when the curly braces are not used, the loop structure can only control one line of code, and we usually add enlarged parentheses
2. While cycle 1. Loop structure [initialization part] while (condition judgment part) {loop body [iterative part]} 2. Cycle characteristics
For while loops, it is usually used when the number of loops is uncertain, such as letting the user decide when to exit the loop.
You can use a Boolean variable to directly determine whether the loop continues or not
You can jump out of the loop at any time with the break keyword
End the cycle when the condition judgment part fails.
Initialization and iteration can be omitted
If a semicolon appears after the conditional judgment part, it will cause the structure to end directly.
3. Sample program
Loop output 1 to 9
Public static void main (String [] args) {/ / initialization part int I = 1; / / conditions for loop execution to continue in parentheses: while (I) when I value is less than or equal to 9
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.