The cyclic control structure of the Java language

Source: Internet
Author: User

Java the loop control structure of language

Cyclic control structure: Under certain conditions, repeated execution of a program of the process structure, is repeatedly executed by the program called the Loop body. The loop control structure is a very important and basic structure in the program, which is implemented by the Loop statement. There are three types of looping statements in Java: The while statement, the Do-while statement, and the for statement.

1.while Statement
The general syntax format for the while statement is as follows:
while (conditional expression)
{Loop body};
Where the return value of the conditional expression is Boolean, the loop body can be either a single statement or a compound statement block. While the execution process of the statement is to determine the value of the conditional expression, if true, then the execution of the loop body, the loop body after the execution of the conditional expression to do the calculation and judgment; when the evaluation condition expression is false, skip the loop body to execute the statement following the while statement. Here is an example of using the while statement to calculate the factorial.
Long result=1
while (n>0)
{
result*=n--;
}

2.do-while statement
do-while The general syntax structure of the statement is as follows:
do
   {loop body}
while (conditional expression);
The use of the Do-while statement is similar to the while statement, unlike the while statement, which is not the first to calculate the bar The value of the piece expression, instead of unconditionally first executing the loop body, and then to determine the value of the conditional expression, if the value of the expression is true, then run the loop body, or jump out of the Do-while loop, execute the following statement. As you can see, the Do-while statement is characterized by its loop body being executed at least once. The following is an example of using the Do-while statement to implement factorial operations.
Long result=1;  
do
{
   result*=n--;
}while (n>0); The
3.for Statement
For statement is in the Java language three loop statements in the strong, use the broader one, its process structure can be see Figure 3.2 (c). The general syntax for the
for statement is as follows:
for (expression one; expression two; expression three)
      {loop body};
For statement execution: First, the expression is evaluated Formula 1, complete the necessary initialization work, and then judge the value of the expression 2, if true, then execute the loop body, after executing the loop body and then return to Expression 3, calculate and modify the loop condition, so that the cycle is over. The second cycle starts with calculating and judging the expression 2, and if the value of the expression is still a value, continue looping, or jump out of the entire for statement to execute the following sentence.
The following is an example of a factorial operation with a for statement.
for (long result=1;result>0;n--)
{
   result*=n;
}

Summary:The For loop applies to the number of known loops, while the while loop applies to the number of unknown loops.

The cyclic control structure of the Java language

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.