Cyclic structure
-
while Loop
-
do while loop
-
for loop
-
An enhanced for loop is introduced in Java 5, which is mainly used for arrays
while Loop
-
while is the most basic loop. Its structure is:
while(Boolean expression){ //Cyclic content }
-
As long as the Boolean expression is true, the loop will continue to execute.
-
In most cases, we will stop the loop. We need a way to invalidate the expression to end the loop
-
In a few cases, the loop needs to be executed all the time, such as the request response listening of the server.
-
If the loop condition is always true, it will cause infinite loop [dead loop]. We should try to avoid dead loop in normal business programming. It will affect the program performance or cause the program to jam and crash!
-
Thinking: calculate 1 + 2 + 3 +... + 100 =?
public class whileDemo01{ public static void main(String[] args){ //Output 1 ~ 100 int i=0; while(i<100){ i++; System.out.println(i); } } }
public class whileDemo03{ public static void main(String[] args){ //Calculate 1 + 2 + 3 ++ 100=? int i=0; int sum=0; while(i<=100){ sum=sum+i; i++; } System.out.println(sum); } }
do... while loop
-
For the while statement, if the condition is not met, it cannot enter the loop. But sometimes we need to perform at least once even if the conditions are not met.
-
The do... While loop is similar to the while loop, except that the do... While loop is executed at least once.
```java do{ //Code statement }while(Boolean expression); ```
-
The difference between while and do... While:
- while is to judge before execution, do... while is to judge after execution
- do... while always ensures that the loop body is executed at least once, which is their main difference.
public class DoWhileDemo01{ public static void main(String[] args){ int i=0; int sum=0; do{ sum=sum+i; i++; }while(i<=100); System.out.println(sum); } }
For loop
-
Although all loop structures can be represented by while or do... While, Java provides another statement - for loop, which makes some loop structures simpler.
-
for loop statement is a general structure that supports iteration. It is the most effective and flexible loop structure.
-
The number of times the for loop is executed is determined before execution. The syntax format is as follows:
for(initialization;Boolean expression;to update){ //Code statement }
-
Exercise 1: calculate the sum of odd and even numbers between 0 and 100
-
Exercise 2: use the while or for loop to output the number that can be divided by 5 between 1-1000, and output 3 numbers per line
-
Print 99 multiplication table
public class ForDemo01{ public static void main(String[] args){ int a=1;//Initialization condition while(a<=100){//Conditional judgment System.out.println(a);//Circulatory body a+=2;//iteration } System.out.println("while End of cycle!"); //Initialize / / condition judgment / / iteration for(int i=1;i<=100;i++){ System.out.println(i); } System.out.println("for End of cycle!"); } }
public class ForDemo02{ public static void main(String[] args){ //Exercise 1: calculate the sum of odd and even numbers between 0 and 100 int oddSum=0; int evenSum=0; for(int i=0;i<=100;i++){ if(i%2!=0){//Odd number oddSum+=i;//oddSum=oddSum+i; }else{//even numbers evenSum+=i; } } System.out.println("Odd sum:"+oddSum); System.out.println("Even sum:"+evenSum); } }
public class ForDemo03{ public static void main(String[] args){ //Exercise 2: use the while or for loop to output the number that can be divided by 5 between 1-1000, and output 3 numbers per line for(int i=0;i<=1000;i++){ if(i%5==0){ System.out.print(i+"\t"); } if(i%(5*3)==0){//Each line System.out.println(); //System.out.print("\n"); } } //println will wrap after output //No line break after print output } }
public class ForDemo04{ public static void main(String[] args){ //Print 99 multiplication table for(int j=i;j<=9;j++){ for(int i=1;i<=j;i++){ System.out.print(j+"*"+i+"="+(j*i)+"\t"); } System.out.println(); } } }
Enhanced for loop
-
Here, we just meet and understand, and then we focus on the use of arrays
-
Java 5 introduces an enhanced for loop that is mainly used for arrays or collections.
-
The syntax format of Java enhanced for loop is as follows:
for(Declaration statement:expression){ //Code sentence }
-
Declaration statement: declare a new local variable. The type of the variable must match the type of the array element. Its scope is limited to the circular statement block, and its value is equal to the value of the array element at this time.
-
Expression: an expression is the name of the array to be accessed, or a method whose return value is an array.
public class ForDemo05{ public static void main(String[] args){ int[] numbers={10,20,30,40,50};//Defines an array for(int i=0;i<5;i++){ System.out.println(numbers[i]); } System.out.println("===================") //Traversing the elements of an array for(int x:numbers){ System.out.println(x); } } }
break,continue,goto
- Break in the main part of any loop statement, you can use break to control the flow of the loop. Break is used to forcibly exit the loop without executing the remaining statements in the loop. (break statements are also used in switch statements)
- The continue statement is used in the body of a loop statement to terminate a loop process, that is, skip the statements that have not been executed in the loop body, and then determine whether to execute the loop next time.
- About goto keyword
- Goto keyword has long appeared in programming languages. Although goto is still a reserved word of Java, it has not been officially used in the language; Java has no goto. However, in the two keywords break and continue, we can still see some shadow of goto -- labeled break and continue.
- "label" refers to the identifier followed by a colon, for example: label:
- For Java, the only place where the label is used is before the loop statement, and the only reason for setting the label before the loop is that we want to nest another loop in it. Because the break and continue keywords usually only interrupt the current loop, but if they are used with the label, they will interrupt to the place where the label exists.
public class BreakDemo{ public static void main(String[] args){ int i=0; while(i<100){ i++; System.out.println(i); if(i==30){ break; } } } }
public class ContinueDemo{ public static void main(String[] args){ int i=0; while(i<100){ i++; if(i%10==0){ System.out.println(); continue; } System.out.print(i); } } }
public class LabelDemo{ public static void main(String[] args){ //Print all prime numbers between 101-150 int count=0; outer:for(int i=101;i<150;i++){ for(int j=2;j<i/2;j++){ if(i%j==0){ continue outer; } } System.out.print(i+" ") } } }
public class TestDemo{ public static void main(String[] args){ //Print 5 lines of triangles for(int i=1;i<=5;i++){ for(int j=5;j>=i;j--){ System.out.print(" "); } for(int j=1;j<=i;j++){ System.out.print("*"); } for(int j=1;j<i;++-){ System.out.print("*"); } System.out.println(); } } }