1. Cycle structure (i):
Syntax:
while (loop condition) {
Looping operations
}
The following columns of code:
int i=1;
while (i<=100) {
SYSTEM.OUT.PRINTLN ("Study hard!! ");
i=i+1;
}
2. What is the circular structure: from the above analysis, we can see that all the circular structure has such characteristics: first, like not endlessly, to meet certain conditions when the cycle will continue, called: "Cycle conditions", the cycle conditions are not satisfied when the cycle exits; A looping structure is a series of operations that repeat the same or similar, called "like operations."
3. Keywords:
> The content in the parentheses after the while is a looping condition. A loop condition is an expression whose value is a Boolean type, which is "true" or "false", such as i<=100. The statements in {} are collectively referred to as circular operations, also known as loop bodies.
The execution order of the >while loop operation is generally as follows:
①. Declare and initialize the loop variable.
②. Determine whether the loop condition is satisfied, and if so, perform a looping operation; otherwise exit the loop.
③. After performing the loop operation, it is not satisfied to judge the cyclic condition again, and decides to continue the loop operation or exit the loop.
In fact, the while loop structure if the first judgment is not satisfied, it will jump directly out of the loop, the loop operation will not be executed again. This is a feature of the while loop structure: first judgment, then execution.
4.do-while Cycle
Grammar:
do{
Looping operations
}while (cyclic conditions);
The following columns of code:
int i=1;
do{
SYSTEM.OUT.PRINTLN ("Study hard!! ");
i++;
}while (i<=100);
5. Keywords: Unlike the while loop, the Do-while loop begins with the keyword do, followed by a loop operation surrounded by curly braces, followed by a loop condition in the while keyword and the enclosed parenthesis. It is important to note that the Do-while loop structure ends with a semicolon.
①: Declaring and initializing a loop variable
②: Perform a loop operation again
③: Determines the loop condition, if the loop condition is met, the loop continues execution, otherwise exits.
The Do-while cycle is characterized by first execution and then judgment.
Depending on the execution of the Do-while loop, it can be seen that the loop operates at least once.
6. The steps to resolve the problem using the Do-while loop are as follows:
①: Analyze cycle conditions and loop operations.
②: Write code in Do-while syntax.
③: Checks if the loop can exit.
7. Contrast
Learn the two cycle structures of while and do-while. What are the similarities and differences between the two types of cyclic structures?
The same point: both are loop structures, which use while (loop condition) to denote loop conditions, which enclose the loop operation in curly braces.
Different points: expressed in the following three aspects.
①: The syntax is different, compared with the while loop structure, the DO-WHILE loop structure puts the while keyword and the loop condition behind, and there are more than one keyword do, followed by a semicolon;
②: The order of execution is different. The while loop structure is first judged, then executed, and the DO-WHILE loop structure is executed first and then judged.
③: In the case where the loop condition is not satisfied at first, the while loop structure does not execute at once, and the DO-WHILE loop structure is executed at least once, regardless of the circumstances.
This chapter summarizes:
The > Cycle structure is composed of cyclic conditions and cyclic operations. As long as the loop condition is met, the loop operation executes repeatedly.
> Steps to solve the problem using loops: Analyze loop conditions and loop operations, write code in the syntax of a loop, and check if loops can exit.
> When writing the cyclic structure code, it is important to note that the initial value of the cyclic variable, the change of the cyclic variable and the loop condition are the relations between the cyclic variables; Make sure that the number of loops is correct and that there is no "dead loop".
>while cycle structure is characterized by first judgment, after execution. Do-while cycle structure is characterized by first execution, then judgment.
> Program debugging is a generic term for tools and methods that meet the functions of suspending programs, observing variables and executing statements. The main methods include setting breakpoints, stepping through, and observing variables.
Java Junior Development Programmer (Unit fifth)