Java for statement (translated from Java tutorials)

Source: Internet
Author: User

From http://www.cnblogs.com/ggjucheng/archive/2012/12/16/2820842.html

English from http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

The for statement provides a compact way to traverse a range value.ProgramIt is often referenced as a "For Loop" because it repeats until a specific condition is met. The common form of the for statement is as follows:

 
For (initialization; termination; increment) {Statement (s )}

When using the for statement of this version, remember:

    • The initialization statement initializes the loop. It is executed once as the beginning of the loop.
    • When the end expression is calculated as false, the loop ends.
    • The auto-increment expression is executed in each iteration of the loop. The expression is in the incrementally and the value is decreasing, which is completely acceptable,

Next programFordemo: uses the common form of the for statement to output 1 to 10 to the standard output:

 
Class fordemo {public static void main (string [] ARGs) {for (INT I = 1; I <11; I ++) {system. out. println ("count is:" + I );}}}

Program output:

Count is: 1 count is: 2 count is: 3 count is: 4 count is: 5 count is: 6 count is: 7 count is: 8 count is: 9 count is: 10

Note:CodeDeclare variables in the initialization expression. The survival range of the variable, from its declaration to the end of the block of the for statement. Therefore, it is commonly used in end expressions and auto-increment expressions. If the for statement is controlled and external variables do not need to be recycled, it is best to declare variables in the initialization block. The names I, j, and K are often used to control the for loop. They are declared in the initialization expression to limit their lifecycles and reduce errors.

The three expressions of the for loop are optional. an infinite loop can be written as follows:

 
// Infinite loopfor (;) {// your code goes here}

 

The for statement can also be used to iterate collections and arrays. This form is sometimes called theenhanced of the for statement. It can be used to make your cycle more compact and easy to read. To illustrate this, consider the following array, which has numbers 1 to 10:

 
Int [] numbers = {1, 2, 4, 5, 6, 7, 8, 9, 10 };

The following programEnhancedfordemo:

Class enhancedfordemo {public static void main (string [] ARGs) {int [] numbers = {1, 2, 4, 5, 6, 7, 8, 9, 10}; For (INT item: numbers) {system. out. println ("count is:" + item );}}}

In this example, the variable item holds the current value of the number array. The output result of the program is the same as before:

 
Count is: 1 count is: 2 count is: 3 count is: 4 count is: 5 count is: 6 count is: 7 count is: 8 count is: 9 count is: 10

Try to use this form of for instead of the traditional for form.

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.