The first type: for loop
The format of the loop structure for statement:
for(initialization expression; conditional expression; operation expression after loop) {
Circulation body;
}
eg:
class Dome_For2{ public static void main(String[] args) { //System.out.println("Hello World!"); //Find the sum of even numbers of 1-10 int sum = 0; for (int i = 1;i<=10 ; i++ ) { if (i%2 ==0) { //Judgement statement sum +=i; //Sum} } System.out.println(sum); }}The output structure is 30
The second while statement
The format of the loop structure while statement:
Initialization statement;
while(judgment conditional statement) {
Loop body statement;
Control condition statement;
}
eg:
class Demo_While { public static void main(String[] args) { //Find the sum of 1-100 int sum = 0; //Define the initial sum to 0 int i = 1; //Define the first number to start sum while (i <= 100) { //Judge conditional statement sum += i; //sum = sum + i; i++; //Let the variable i increase itself} System.out.println("sum = " + sum); }}The output result is: sum = 5050
The third do....while statement
The format of the loop structure do...while statement:
Initialization statement;
do {
Loop body statement;
Control condition statement;
}while(judgment conditional statement);
eg:
class Demo1_DoWhile { public static void main(String[] args) { //Find the sum of 1-100 int sum = 0; //Define the variable sum, which is used to store the sum of the value int i = 1; //Define the variable i do { //Do is the stem//System.out.println("i = " + i); //Loop body statement sum +=i; i++; } while (i <= 100); //Judge the conditional statement System.out.println("sum = "+sum); //Output result} }Output result: sum = 5050
Summary: The difference between three loop statements:
1. The do...while loop executes the loop body at least once.
2. For, while loop must first determine whether the condition is true and then decide whether to execute the loop body statement.
The above introduction to the difference between the three loop sentences in Java language - is the entire content shared by the editor. I hope it can give you a reference and I hope you can support Wulin.com more.