Java language-differences between three types of loop statements and java-three types of statements
------- Android training, java training, and hope to communicate with you! ----------
First: for Loop
Loop Structure for statement format: for (initialization expression; conditional expression; Operation expression after loop) {loop body;} eg:
1 class Dome_For2 {2 public static void main (String [] args) {3 // System. out. println ("Hello World! "); 4 // calculate the even numbers of 1-10 and 5 int sum = 0; 6 for (int I = 1; I <= 10; I ++) {7 if (I % 2 = 0) {// judge Statement 8 sum + = I; // sum 9} 10} 11 System. out. println (sum); 12} 13}
The output structure is 30.
The second while statement
Loop structure while statement format:
Initialization statement;
While (Judgment Condition Statement) {loop body statement; Control Condition Statement;} eg:
1 class Demo_While {2 public static void main (String [] args) {3 // calculate the sum of 1-100 4 int sum = 0; // define the initial sum as 0 5 int I = 1; // define the first number of 6 while (I <= 100) {// judgment Condition Statement 7 sum + = I; // sum = sum + I; 8 I ++; // Let variable I auto increment 9} 10 System. out. println ("sum =" + sum); 11} 12}
Output result: sum = 5050
Third do... while statement
Loop Structure do... while statement format:
Initialization statement; do {loop body statement; Control Condition Statement;} while (Judgment Condition Statement); eg:
1 class demodomaindowhile {2 public static void main (String [] args) {3 // evaluate the sum of 1-and 4 int sum = 0; // define the variable sum, 5 int I = 1; // defines the variable I 6 do {// do is dry 7 // System. out. println ("I =" + I); // loop body Statement 8 sum + = I; 9 I ++; 10} 11 while (I <= 100 ); // judgment Condition Statement 12 System. out. println ("sum =" + sum); // output result 13} 14}
Output result: sum = 5050
Summary:
The differences between the three loop statements:
1. do... while loop executes at least one loop body.
2. for a while loop, you must first determine whether the condition is true, and then decide whether to execute the loop body statement.
------- Windows Phone 7 Mobile Phone development,. Net training, and we look forward to communicating with you! -------