I. Four Elements of the loop structure:
1. Initial Variables
2. Iteration variable
3. Cyclic Conditions
4. cyclic Operation (Cyclic body)
A loop condition is an expression whose value is of the boolean type, either true or false.
Ii. Basic syntax of the loop structure:
1. While Loop Structure
While (loop condition ){
// Cyclic Operation (Cyclic body)
}
Feature: first judge whether the cycle condition is met, and then execute the loop operation!
Example:
Public static void main (string [] ARGs ){
// Initial variable: qualified
Boolean flag = false; // The default value is false.
While (flag = false) {// loop Condition
System. Out. println ("reading materials in the morning, coding in the afternoon ");
System. Out. println ("Qualified? (y/n )");
Wrote input = new partition (system. In );
String choice = input. Next (); // y
If (choice. Equals ("Y ")){
Flag = true; // change the value of the iteration variable
}
}
System. Out. println ("Enter y to exit the loop! ");
}
}
2. Do-while loop structure
Do {
// Cyclic Operation
} While (Cyclic condition );
Features: perform a loop operation first, and then determine whether the cycle conditions are met. When the cycle conditions are met, continue to perform the loop operation!
Example:
Public static void main (string [] ARGs ){
Wrote input = new partition (system. In );
// Enter the encoding before determining whether the encoding is successful. If the encoding is not successful, continue encoding.
String choice = "";
Do {
System. Out. println ("encoding completed ");
System. Out. println ("qualified: (y/n )");
Choice = input. Next (); // y
} While (! Choice. Equals ("Y "));
System. Out. println ("Enter y to exit the loop! ");
}
}
3. For Loop Structure
For (expression 1; expression 2; expression ){
// Cyclic Operation (Cyclic body)
}
Expression 1: indicates the initial variable.
Expression 2: indicates the cyclic condition.
Expression 3: iteration variable
All three expressions can be omitted! However, try not to omit each expression!
Features: it is mainly used for loops with fixed cycles!
Example:
// Calculate 1 ~ The sum of the numbers not divisible by 3 between 100
Public static void main (string [] ARGs ){
// Define a sum variable to save the sum of values not divisible by 3
Int sum = 0;
For (INT I = 1; I <= 100; I ++ ){
// Determine whether a variable can be divisible by 3
If (I % 3! = 0 ){
Sum = sum + I;
}
}
// Out of the loop, the sum is saved as the desired value
System. Out. println ("Total" + sum );
}
}
Iii. Jump statement
1. Break
The next statement used to terminate a loop and redirect the program to the external body of the loop. In a loop, the statement after break is not executed.
It is usually used with the if condition statement!
Example:
Public static void main (string [] ARGs ){
// The original loop needs to be executed 10 times, but when it reaches the third time, I want to terminate the entire
For (INT I = 1; I <= 10; I ++ ){
If (I = 3 ){
// Execute the loop. The {} Where the loop exists. You can use break.
Break;
}
System. Out. println (I );
}
System. Out. println ("you cannot see the number after ");
The running result is: 1.
2
You cannot see the number after the three Yishu
2. Continue
The statement used to jump out of this loop and enter the next loop will not jump out of the loop structure!
Example:
Public static void main (string [] ARGs ){
// The original loop needs to be executed five times, but when it reaches the third time,
// I want nothing to output, and then continue the loop!
For (INT I = 1; I <= 5; I ++ ){
If (I = 3 ){
// Execute the loop. The {} Where the loop exists. You can use break.
Continue;
}
System. Out. println (I );
}
System. Out. println ("You cannot see 3 ");
The running result is: 1.
2
4
5
You cannot see 3
Iv. program debugging
1. Definition
Tools and methods for pausing programs and observing variables and executing statements one by one are called program debugging!
2. Purpose
Identify the cause of the defect and correct it!
3. Procedure
01. Analysis Error. Set the breakpoint
02. Start debugging and perform it step by step.
03. Observe the variable value and modify the error code.
V. Differences between the three cycle Structures
1: syntax
01, while loop structure:
While (loop condition ){
// Cyclic body (cyclic Operation)
}
02. Do-while loop structure:
Do {
// Cyclic Operation
} While (Cyclic condition );
03. For Loop Structure:
For (expression 1; expression 2; expression 3 ){
// Cyclic Operation
}
2: execution sequence
While loop: first judge and then execute
Do-while loop: Execute first and then judge
For Loop: first judge and then execute
3: Applicable
When the number of cycles is determined, the for loop is usually used.
When the number of loops is uncertain, the while or do-while loop is usually used.