Well-off will accompany you to learn JAVA -------- The For loop of the three major loops, java -------- three major

Source: Internet
Author: User

Well-off will accompany you to learn JAVA -------- The For loop of the three major loops, java -------- three major

When you clearly know the number of times a loop is executed, you can use the for loop. The statement format is as follows:

If there are only one statement to be processed in the loop body, you can remove the braces. The for loop process is listed below.

1. When you enter the for loop for the first time, assign the starting value to the loop control variable.

2. check whether or not to continue executing the Loop Based on the content of the judgment condition. When the judgment condition value is true, continue to execute the statements in the loop body. The judgment condition value is false) then, the system will jump out of the loop and execute other statements.

3. After the statement in the loop body is executed, the loop control variable changes the value of the loop control variable according to the increase or decrease requirement, and then returns to step 2 to determine whether to continue the loop.

01 // The following program describes how to use the for Loop

02 public class TestJava3_30

03 {

04 public static void main (String [] args)

05 {

06 int I, sum = 0;

07 // use of the for loop to calculate the sum of accumulated numbers

08 for (I = 1; I <= 10; I ++)

09 sum + = I; // calculate sum = sum + I

10 System. out. println ("1 + 2 +... + 10 =" + sum );

11}

12}

Output result:

1 + 2 +... + 10 = 55

Program description:

1. Declare two variables sum and I in row 06th. I is used for the Count control of the loop.

2. 08 ~ Line 09, do 1 ~ The execution result is TestJava3_28. If you do not understand the result, you can compare it with the TestJava3_28 program. I believe you can understand the usage of.


For Loop statements in java

Oh, this is JDK1. 5 new features for (1: 2) 1 must be type Object Name: Set Name
This new feature is generally used together with the set generic
The function is to traverse the Set 2.
You can use a 1-type object without traversing it once.

For Loop statements in java

For (int I = 0; I <50; I + 3)
{
}

This is wrong.
You should write it like this
For (int I = 0; I <50; I = I + 3)
{
}

If you want to say that, you should do this.
Int y = 10
For (int I = 0; I <10; I ++)
{
A [I] = y;
Y = y-1;

}

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.