The first type: For loop
The format of the loop structure for statement:
for (initialization expression; conditional expression; loop action expression) {
Circulation body;
}
eg
Class dome_for2{public
static void Main (string[] args) {
//system.out.println ("Hello world!");
Find 1-10 even and
int sum = 0;
for (int i = 1;i<=10 i++) {
if (i%2 ==0) { //Judgment statement
sum +=i; Sum
}
System.out.println (sum);
}
Output structure is 30
The second while statement
The format of the while statement of the loop structure:
Initialization statement;
while (judgment condition statement) {
Circular body statement;
Control conditional statement;
}
eg
Class Demo_while {public
static void Main (string[] args) {
//seek 1-100 and
int sum = 0; Defines the initial and 0
int i = 1; Defines the first number of sums to start while
(i <=) { //Judge conditional statement
sum = i; sum = sum + i;
i++; Let the variable i self increase
}
System.out.println ("sum =" + sum);
}
The output is: sum = 5050
The third kind of do....while statement
Loop structure do...while statement format:
Initialization statement;
do {
Circular body statement;
Control conditional statement;
}while (judging conditional statement);
eg
Class Demo1_dowhile {public
static void Main (string[] args) {
//seek 1-100 and
int sum = 0; Defines the variable sum, which is used to store the sum of the value
int i = 1; The definition of
the variable i do {//does is the dry
//system.out.println ("i =" + i); Loop body statement
sum +=i;
i++;
}
while (i <=); Judge Conditional statement
System.out.println ("sum =" +sum); Output results
}
}
Output result: Sum = 5050
Summary: The difference between three kinds of circular statements:
1. The Do...while loop executes at least once for the loop body.
2, and the For,while cycle must first determine whether the condition is set up, and then decide whether to execute the Loop body statement.
The above Java language----Three kinds of circular statements of the difference is a small series to share all the content, I hope to give you a reference, but also hope that we support the cloud-dwelling community.