Java looping structure-for, while and Do...while__java

Source: Internet
Author: User
the sequential structure of a program statement can only be executed once. If you want the same operation to execute multiple times, you need to use a looping structure.

There are three main looping structures in Java: While loops do...while loops for loops

An enhanced for loop that is primarily used in arrays is introduced in Java5. While Loop

While is the most basic loop, its structure is:

while (Boolean expression) {
  //loop content
}

The loop experience executes as long as the Boolean expression is true. instance Test.java file Code:

public class Test {public
   static void Main (String args[]) {
      int x = ten;
      while (x <) {
         System.out.print ("Value of x:" + x);
         x + +;
         System.out.print ("\ n");}}

The results of the above instance compilation run are as follows:

Value of X:10
value of x:11
value of x:12 value of x:13 value of
x:14
20/>value of x:16 value of
x:17
value of x:18 value of
x:19
Do...while Cycle

For a while statement, you cannot enter a loop if the condition is not met. But sometimes we need to do it at least once, even if we don't meet the conditions.

The Do...while loop is similar to the while loop, and the Do...while loop executes at least once.

do {
       //code Statement
}while (boolean expression);

Note: The Boolean expression is behind the loop body, so the statement block is executed before the Boolean expression is instrumented. If the Boolean expression evaluates to True, the statement block executes until the value of the Boolean expression is false. instance Test.java file Code:

public class Test {public
   static void Main (String args[]) {
      int x = ten;
 
      do{
         System.out.print ("Value of x:" + x);
         x + +;
         System.out.print ("\ n");
      } while (x <);
   }
}

The results of the above instance compilation run are as follows:

Value of X:10
value of x:11
value of x:12
value of x:13
value of x:14
value of x:15
VA Lue of x:16 value of
x:17
value of x:18 value of
x:19
For Loop

Although all loop structures can be represented by a while or Do...while, Java provides another statement--for loop, making some loop structures simpler.

The number of execution times for a for loop is determined before it is executed. The syntax format is as follows:

for (initialization; Boolean expression; update) {
    //code statement
}

There are several instructions about the For loop: first to perform the initialization step. You can declare a type, but you can initialize one or more loop control variables or empty statements. The value of the Boolean expression is then instrumented. If true, the loop body is executed. If False, the loop terminates and begins execution of the statement following the loop body. After you perform a loop, update the loop control variable. Detects a Boolean expression again. Loop through the process above. instance Test.java file Code:

public class Test {public
   static void Main (String args[]) {for
 
      (int x = x < x = x+1) {
         SYSTEM.O Ut.print ("Value of x:" + x);
         System.out.print ("\ n");}}

The results of the above instance compilation run are as follows:

Value of X:10
value of x:11
value of x:12
value of x:13
value of x:14
value of x:15
VA Lue of x:16 value of
x:17
value of x:18 value of
x:19
Java enhanced for Loop

JAVA5 introduces an enhanced for loop that is primarily used for arrays.

The Java enhanced for loop syntax format is as follows:

For (Declaration statement: expression)
{
   //code sentence
}

Declaration statement: Declares a new local variable with the type of the variable that must match the type of the array element. Its scope is scoped to the circular statement block, and its value is equal to the value of the array element at this time.

Expression: An expression is the name of the array to be accessed, or a method that returns a number of values. instance Test.java file Code:

public class Test {public
   static void Main (String args[]) {
      int [] numbers = {A, m, n};
 
      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 instance compilation run are as follows:

10,20,30,40,50,
james,larry,tom,lacy,
Break keyword

Break is mainly used in loop statements or switch statements to jump out of the entire statement block.

The break jumps out of the innermost loop and continues with the statement below the loop.

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.