First type: For loop
Loop structure for statement format:for (initialize an expression; a conditional expression; an action expression after a loop) {circulation body; }eg:
1 class dome_for2{2 public static void Main (string[] args) {3 //system.out.println ("Hello world!"); 4 //Request 1- 10 even and 5 int sum = 0; 6 for (int i = 1;i<=10; i++) {7 if (i%2 ==0) { //Judgment statement 8 sum +=i;
//summation 9 }10 }11 System.out.println (sum); }13}
The output structure is 30
Second while statement
The format of the loop structure while statement:
initialization statements;
While (judging condition statement) {loop body statement;control condition statement;}eg
1 class Demo_while {2 public static void Main (string[] args) {3 //Seek 1-100 and 4 int sum = 0; Defines the initial and is 0 5 int i = 1; Defines the first number of start sums 6 while (i <=) { //Judgment condition Statement 7 sum + = i; sum = sum + i; 8 i++; Let the variable i increment by 9 }10 System.out.println ("sum =" + sum); }12}
The output is: sum = 5050
The third type of Do....while statement
The format of the loop structure Do...while statement:
initialization statements;Do {loop body statement;control condition statement;}while (Judgment condition statement);eg:
1 class Demo1_dowhile {2 public static void Main (string[] args) {3 //Seeking 1-100 and 4 int sum = 0; Defines the variable sum, which is used to store the sum value 5 int i = 1; Define the variable i 6 do {//does is dry 7 //system.out.println ("i =" + i); Loop Body Statement 8 sum +=i; 9 i++;10 }11 while (i <=); Judge Conditional statement System.out.println ("sum =" +sum); Output results }14 }
Output result: Sum = 5050
Summarize:
The difference between the three circular statements:
The 1.do...while loop executes at least one loop body.
2. The For,while cycle must first determine whether the condition is true, and then decide whether to execute the Loop body statement.
The difference between three types of cyclic structures in Java