Loop structure
The loop structure is used when the same code or similar code needs to be executed repeatedly.
Three elements of the loop:
(1) Declaration of a cyclic variable: a cyclic factor used to control the number of cycles
(2) Cyclic condition: a condition used to determine whether to perform the same or similar content (loop body)
(3) Changing direction of the cyclic variable: changing direction toward the end of the cycle. 、
(1) For loop:
Grammar:
for (Declaration and initialization of variables; loop condition; change direction of variable) {
Circulation body;
}
Execution logic:
When a program encounters a for, it must execute the declaration and initialization of the variable, then execute the loop condition to judge, and if False, skip the loop structure and execute the subsequent code. If true, the loop body is executed, then the variable is executed, then the cyclic condition is judged, ....
Loop body: Is the same or similar logic to be executed repeatedly
Break Keyword: Indicates the end/interrupt loop when used in a loop structure
continue Keyword: used in the loop structure, the end of the secondary loop body, continue the next cycle
Double-layer for loop:
The For loop has a for loop nested, the outer variable executes once, the inner variable executes one pass or the outer variable controls the number of rows, the inner variable controls the number of columns
Practice Code:
Public Static voidMain (string[] args) {//print a hollow isosceles triangle intN=6; for(inti=0;i<n;i++){ for(intj=i;j<n;j++) {System.out.print (" "); } System.out.print ("*"); if(i!=n-1){ for(intx=i*2-1;x>0;x--) {System.out.print (" "); } if(i!=0) {System.out.print ("*"); } }Else{ for(intk=0;k< (n*2-2); k++) {System.out.print ("*"); }} System.out.println (); } }(2) While loop
Grammar:
While (loop condition) {
Loop body
}
Execution logic:
When the program encounters a while, the loop condition must be executed, if the result is false, the loop structure is terminated, the subsequent code is executed, if the result is true, the loop body is executed, and then the loop condition is judged ...
Practice Code:
Public Static voidMain (string[] args) {/*print 10 times Hello World*/ intCount = 1; while(count<=10) {System.out.println ("Hello World"); Count++; } /*The second common notation*/ intnum = 1; while(true) {System.out.println ("Hello World"); Num++; if(num>10){ Break; } } }(3) Do-while cycle
Grammar:
do{
Loop body
}while (cyclic conditions);
Execution logic: When the program encounters the DO keyword, it must first execute the loop body, then determine the loop condition, if the condition is false, end the loop structure, execute subsequent code, if the condition is true, then execute the loop body again, and then determine whether the condition is set up or not ....
when/do-while/ for differences:
While/do-while: For logic that does not know the number of cycles
For: logic for knowing the number of loops
While/for generally judge the condition first, then execute the loop body
Do-while: Be sure to first execute the loop body before judging the condition
Usage tips: When to use while, when to use do-while some logic can be translated as follows:
When...... To carry On ... : Suitable for use while
Do..... Until ...: Suitable for use with Do-while
Practice Code:
Public Static voidMain (string[] args) {/*bank card password is 888888 use do-while analog ATM input password operation*/Scanner SC=NewScanner (system.in); intinput =-1; Do{System.out.println ("Please enter your password:"); Input=Sc.nextint (); } while(input!=888888); System.out.println ("Password verification succeeded"); }
Java Basics (5)-Looping statements