Java for Loop statement

Source: Internet
Author: User
Tags clear screen variables

The For loop is initialized before the first iteration. It then tests the condition and, at each time of repetition, carries out some form of "stepping" (stepping). The For loop forms the following:

for (initial expression; Boolean expression; stepping)
Statement

Either the initial expression, the Boolean expression, or the step, can be empty. Test the Boolean expression before each iteration. If the result is false, it continues to execute the line of code immediately following the for statement. At the end of each loop, the step is computed once.
The For loop is typically used to perform the Count task:

: Listcharacters.java
//demonstrates ' for ' loop by listing
//All of the ASCII characters.

public class Listcharacters {public
  static void Main (string[] args) {for
  (char c = 0; c < 128; C +)
    if !=)  //ANSI Clear screen
      System.out.println (
        "Value:" + (int) C +
        "character:" + C);
  }
} ///:~


Note that variable c is defined when it is needed-within the control expression of the For loop, rather than at the beginning of the code block marked by the opening curly brace. The scope of C is an expression controlled by for.
In a traditional programmed language like C, all variables are required to be defined at the beginning of a block. So when the compiler creates a block, it can allocate space for those variables. In Java and C + +, you can scatter variable declarations across the entire block to define where they really need to be. This will create a more natural coding style, and easier to understand.
You can define multiple variables in a for statement, but they must have the same type:

for (int i = 0, j = 1;
    I < ten && J!=;
    i++, J + +)/* Body of the For
 loop * *;


where the int definition within the FOR statement overwrites both I and J. Only for loops have the ability to define variables in a control expression. You cannot use this method for any other condition or loop statement.

1. Comma operator
As early as in the 1th chapter, we have mentioned the comma operator-note that it is not a comma delimiter; the latter is used to delimit different arguments of a function. The only place in Java where the comma operator is used is the control expression for the For loop. In the initialization and stepping control parts of the control expression, we can use a series of statements separated by commas. And those statements will be executed independently. The previous example has used this capability, and here is another example:

: Commaoperator.java public

class Commaoperator {public
  static void Main (string[] args) {
    for (int i = 1, j = i + 10; I < 5;
        i++, j = i * 2) {
      System.out.println ("i=" + i + "j=" + j);
    }
  }
} ///:~


The output is as follows:

i= 1 j=
i= 2 j= 4
i= 3 j= 6
i= 4 j= 8


As you can see, the statements are executed sequentially, both in the initialization and in the stepping section. In addition, although the initialization section can set any number of definitions, it belongs to the same type.

Related Article

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.