Java syntax (6): loop

Source: Internet
Author: User
Java syntax (6): general Linux technology-Linux programming and kernel information. For details, see the following section. Loop: for, while, and do
Java has three types of loop control statements: for statement, while statement, and do statement. The following describes the structure of these three statements.

1: for Loop

The format of the for statement is:

For (initialization statement; Condition Statement; control statement)
{
Statement 1;
Statement 2;
....

Statement n;
}

The execution sequence of the for statement is: first execute the "initialization statement", then test the "conditional statement". If the condition is true, execute the 1 to n statements, and then execute the "control" statement; then, test whether the condition statement is true. If the condition statement is true, repeat the preceding process until the condition is invalid. For example:

For (I = 0; I <10; I ++ )......;
Int I, a [] = new int [10];
For (I = 0, I <10; I ++) = 0;

This Code assigns all elements in integer array a to 0.
You can describe your variables in the header of the for loop, and the last expression can be omitted. However, make sure that the value of the variable is changed in the statement, for example:

For (int I = 0; I <= 10;) I + = I;
In a for loop, the "initialization statement", "Condition Statement", and "control statement" can be omitted, but the semicolon in the for loop cannot be omitted. For example:

Int I = 0;
For (;;;)
{
If I> 10 break;
I = I + 1;
}

When the "Condition Statement" is omitted in a for loop, the for statement {} must be replaced by a sentence. The control program jumps out of the for loop when a condition is met. Otherwise, an endless loop is formed.

2: while LOOP

The while loop is similar to the for loop in the format:

While (Condition Statement)
{
Statement 1;
Statement 2;
....

Statement n;
}

When running the while statement, first test the "Condition Statement". If the condition is true, execute the statements 1 to n until the condition is not met, and then place the loop.

Int I = 0;
While (I <10)
{
I ++;
System. out. println ("Hey !. Get me out of here! :);
}

3: do... while loop

Do... while loop statement format:
Do
{
Statement 1;
Statement 2;
....

Statement n;
}
While (Condition Statement );

The function of the do... while statement is to first execute Statement 1 to statement n, and then perform a condition test. If the condition is true, continue to execute Statement 1 to statement n. Otherwise, this jumps out of the loop. For example:

Boolean test = false;
Do
{
......

}
While (test );

This kind of control is not very common, but sometimes it is very important. Pay attention to the semicolon after the while statement at the end.
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.