Loop Structure:
Loop is done repeatedly; when conditions are established, what is done continuously.
Features:
The loop is not endless. The loop will continue only when certain part adjustment conditions are met. It is called the "loop condition". When the loop condition is not met, the loop will exit;
A loop structure is a series of identical or similar operations that are called "cyclic operations ".
Java program has three implementation methods:
While loop structure; do_while loop structure; for loop structure.
While loop structure:
The content in the parentheses after the keyword while is a loop condition. A loop condition is an expression whose value is of the boolean type, that is, "true" or "false ". The statements in {} are collectively called cyclic Operations, also known as cyclic bodies.
If the while loop structure is determined for the first time that the loop condition is not met, the loop will jump out directly and the loop operation will not be executed once. This is a feature of the while loop structure: first judgment, then execution.
While loop structure execution sequence:
1. Declare and initialize the cyclic variable.
2. Determine whether the cyclic conditions are met. If the conditions are met, perform the cyclic operation; otherwise, exit the loop.
3. After the cyclic operation is completed, judge the cyclic conditions again and execute the loop in sequence or exit the loop.
Common Errors:
1. Analyze the cyclic conditions and operations.
2. Use the while syntax to write the code.
3. Check whether the loop can exit.
Program debugging:
In order to find out the problem in the program, you want the program to pause where needed, so that you can see that running here is the value of the variable. You also want to run the program in one step, track the running process of the program, and observe that the statement is executed, but the statement is not executed.
Tools and methods that meet the functions of suspending programs, observing variables, and executing statements one by one are called program debugging.
How to debug the program:
1. Analysis Error. Set the breakpoint. (When the breakpoint is used for debugging, determine that the program is in a piece of code to detect program errors .)
2. Start debugging and perform it in one step.
Errors or defects in computer programs are often referred to as "bugs" and program debugging as "debug", which means discovering and solving bugs.
Note the following when writing the cyclic structure code:
The relationship between the initial values of the cyclic variables, the changes in the cyclic variable values in the cyclic operation, and the cyclic conditions ensures that the number of cycles is correct and there is no "endless loop ".
The execution sequence of the do_while loop:
1. Declare and initialize the cyclic variable.
2. Perform a loop operation.
3. Judge the cycle condition. If the cycle condition is met, the loop continues to be executed; otherwise, the loop is exited.
The difference between a while loop and a do_while loop:
Same:
All are loop mechanisms. "While (loop condition)" is used to indicate the loop condition and braces are used to enclose the loop operation.
Differences:
1. syntax is different. Compared with the while loop structure, the do_while loop structure places the while keyword and the loop condition behind it, and adds the keyword do and a semicolon.
2. the execution order is different. The while loop structure is judged first, and executed; the do_while loop structure is executed first, and then determined.
3. If the first loop condition is not met, the while loop structure will not be executed once, And the do_while loop structure will be executed at least once in any case.
Chapter 5 cyclic structure