In this paper, we introduce the difference between the for-, while and do-three circular statements in Java, which are shown in the following detail:
The first type: For loop
The format of the loop structure for statement:
for (initialization expression; conditional expression; Operation expression after loop) {
loop 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 (judging conditional statement) {
circular body statement;
Control conditional statement;
}
eg
Class Demo_while {public
static void Main (string[] args) {
//seek 1-100 and
int sum = 0;//define initial and 0
int i = 1; /define the first number of sums to start while
(i <= 100) {//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 {
loop body statement;
Control conditional statement;
}while (judging conditional statement);
eg
Class Demo1_dowhile {public
static void Main (string[] args) {
//1-100 and
int sum = 0;//define variable sum to store the sum value C22/>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 <= 100); Judge Conditional statement
System.out.println ("sum =" +sum); Output Results
}
Output result: Sum = 5050
Summary: The difference between three kinds of circular statements:
The 1.do...while Loop performs at least one loop body at a time.
2. The For,while cycle must first determine whether the condition is valid, and then decide whether to execute the Loop body statement.
The above is a small set of Java for you to introduce, while, do while three kinds of circular statements of the difference, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!