Java Review 5 Java looping structure-for, while and Do...while

Source: Internet
Author: User

as far as I know, there are looping statements in programming languages: for, while and Do...while, here is the difference, 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>//Inference First Re-loop, infer whether X is less than 9, set up the loop, does not set the 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>}}
Execution Result:

No results

do...while:

public class Xunhuantest {public static void main (String args[]) {      int x = ten;      First loop in inference. First loop once, output value of x:10, in inferring whether X is less than 9. The establishment of the cycle. Does not establish a      non         -cyclic do{System.out.print ("Value of x:" + x);         x + +;         System.out.print ("\ n");      } while (x < 9);}   }

Execution Result:

value of X:10

From the previous two examples. Can see the difference between while and Do...while:

While the first inference is recycled. do...while first loops in inference, that is, running at least once.

In the actual encoding operation. The most used or for loop:

public class Xunhuantest {public static int x = 10;public static void Main (String args[]) {//For loop, loop out value between 10-14 (contains 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 value between 10-14 (contains 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 execution results are:

value of X:10
Value of X:11
Value of X:12
Value of X:13
Value of X:14

Ability to see the For loop features:

    • First run the initialization step. The ability to declare and initialize one or more loop control variables, or an empty statement.

    • And then. Detects the value of a Boolean expression. assumed to be true. The loop body is run. Assuming false, the loop terminates and starts the statement following the loop body.
    • After running the loop once. Updates the loop control variable.

    • Check the Boolean expression again. Loop to run 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 Cycle. Loop out the value between 10-14 (including 10 and +) for (; x < + + x + +) {for (; y<15;y++) {System.out.print ("Value of x:" + x); System.out.print ("Value of y:" + y); System.out.print ("\ n");}}}
Execution 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 above example compiles execution results such as the following:
10,20,30,40,50,james,larry,tom,lacy,
can see:
For (Declaration statement: expression) {   //code sentence}

declaration statement: Declares a new local variable. The type of the variable must match 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 be interviewed, or the method that returns the number of values.

look again at break continue keywordBreakkeyword

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 to run the statement below the loop.

Grammar

The use of break is very easy, 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 above example compiles execution results such as the following:

1020
Continuekeyword

The Continue is suitable for any loop control structure.

The function is to let the program jump to the next iteration of the loop immediately.

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 jumps immediately to the inference 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 above example compiles execution results such as the following:

10204050
Let's say you want to know if statements and IfElse statements look: http://www.w3cschool.cc/java/java-if-else-switch.html





Java Review 5 Java looping structure-for, while and Do...while

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.