as far as I know, there are looping statements in programming languages: for, while and Do...while, here is the difference between them, I do not like to use the language, we look at the code: coding ...
While and do...while differences:
While
public class Xunhuantest {<span style= "white-space:pre" ></span>public static void Main (String args[]) {< Span style= "White-space:pre" ></span> int x = 10;<span style= "White-space:pre" ></span>//First judgment Re-cycle, determine whether X is less than 9, set up a cycle, does not set up a loop <span style= "White-space:pre" ></span> while (X < 9) {<span style= "White-spa Ce:pre "></span> System.out.print (" Value of x: "+ x); <span style=" White-space:pre "></span> X++;<span style= "White-space:pre" ></span> System.out.print ("\ n"); <span style= "White-space:p Re "></span>}<span style=" White-space:pre "></span>}}
Operation Result:
No results
do...while:
public class Xunhuantest {public static void main (String args[]) { int x = ten; First cycle in judgment. First loop once, output value of x:10, in judging whether X is less than 9, set up the loop, does not set the loop do{ System.out.print ("Value of x:" + x); x + +; System.out.print ("\ n"); } while (x < 9);} }
Operation Result:
value of X:10
From the last two examples, you can see the difference between while and Do...while:
while first judging the recirculation,Do...while first loop in the judgment, that is, at least once.
In the actual encoding operation, the most used is the FOR loop:
public class Xunhuantest {public static int x = 10;public static void Main (String args[]) {//For loop, loop out values between 10-14 (including 10 and +) F or (x = x; x < + x + +) {System.out.print ("Value of x:" + ×); System.out.print ("\ n");}}
public class Xunhuantest {public static int x = 10;public static void Main (String args[]) {//For loop, loop out values between 10-14 (including 10 and +) F The OR (; x <; + x + +) {System.out.print ("Value of x:" + ×); System.out.print ("\ n");}} <strong style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " > </strong>
The results of the operation are:
value of X:10
Value of X:11
Value of X:12
Value of X:13
Value of X:14
You can see the For loop features:
- The initialization step is performed first. One or more loop control variables can be declared and initialized, or they can be empty statements.
- Then, the value of the Boolean expression is detected. If true, the loop body is executed. If False, the loop terminates, starting execution of the statement following the loop body.
- After the loop is executed, the loop control variable is updated.
- Re-detects the Boolean expression. The loop executes the above procedure.
For
loop nesting:
public class Xunhuantest {public static int x = 10;public static int y = 10;public static void Main (String args[]) {//For Loop, loop out the value between 10-14 (including 10 and +) for (; x < n; + +) {for (; y<15;y++) {System.out.print ("Value of x:" + x); System.out.print ("Value of y:" + y); System.out.print ("\ n");}}}
Operation Result: value of X:10value of Y:10
Value of X:10value of Y:11
Value of X:10value of Y:12
Value of X:10value of y:13
Value of X:10value of y:14
Java enhanced for loop
public class Test {public static void Main (String args[]) { int [] numbers = {Ten, +, +, +; for (int x:numbers) { System.out.print (x); System.out.print (","); } System.out.print ("\ n"); String [] Names ={"James", "Larry", "Tom", "Lacy"}; for (String name:names) { System.out.print (name); System.out.print (",");}}}
The results of the above example compilation run as follows:
10,20,30,40,50,james,larry,tom,lacy,
Can be seen:
For (Declaration statement: expression) { //code sentence}
declaration statement: Declares a new local variable that must have a type that matches the type of the array element. Its scope is scoped to the Loop statement block, whose value is equal to the value of the array element at this time.
Expression: The expression is the name of the array to access, or is the method that returns the value to the group.
and look at the break continue keywordThe break keyword
Break is used primarily in loop statements or switch statements to jump out of the entire block of statements.
Break jumps out of the innermost loop, and continues execution of the statement below the loop.
Grammar
The use of break is simple, which is a statement in the loop structure:
Break
Instance
public class Test {public static void Main (String args[]) { int [] numbers = {Ten, +, +, +; for (int x:numbers) { if (x = =) {break ; } System.out.print (x); System.out.print ("\ n");}}
The results of the above example compilation run as follows:
1020
Continue keywords
The Continue is suitable for any loop control structure. The function is to let the program jump immediately to the next iteration of the loop.
In the For loop, the continue statement causes the program to jump immediately to the UPDATE statement.
In the while or Do...while loop, the program immediately jumps to the judgment statement of the Boolean expression.
Grammar
Continue is a simple statement in the loop body:
Continue
Instance
public class Test {public static void Main (String args[]) { int [] numbers = {Ten, +, +, +; for (int x:numbers) { if (x = =) { continue; } System.out.print (x); System.out.print ("\ n");}}
The results of the above example compilation run as follows:
10204050
If you want to know about the IF statement and the IfElse statement, see: http://www.w3cschool.cc/java/java-if-else-switch.html
Java Review 5 Java looping structure-for, while and Do...while