Java programming things 37-for statement syntax

Source: Internet
Author: User

Java programming those things 37-for statement syntax Zhengzhou game college Chen yuefeng from: http://blog.csdn.net/mailbomb 5.4.3 for statementThe for keyword indicates "When... It is a commonly used loop statement in actual development. Its Syntax format is slightly more complex than the previous loop statement, but after you are familiar with it, we will find that its syntax is more organized, and the loop control and the loop body are clearly separated. The syntax format of the for statement is: For (initialization statement; loop condition; iteration statement) {loop body;} syntax description: 1. Same as other flow control statements, braces in a statement are not required by syntax. However, to make the structure clear and write multiple lines of code in the loop body, braces are generally used. 2. the initialization statement is executed before the cycle starts. Generally, the code for variable Initialization is written, such as the declaration and assignment of cyclic variables. This statement can be empty. 3. The cyclic condition is a condition for loop establishment. It must be of the boolean type. If this condition is null, the default value is true, that is, the condition is true. 4. Iterative statements refer to statements with variable changes. They generally write structures like I ++ and I-. Of course, this statement can also be empty. 5. The loop body refers to the function code that is executed repeatedly in a loop. Execution Process: 1. Execute initialization Statement 2. Judge the cycle condition. If the cycle condition is false, the cycle ends, otherwise, execute next step 3, execute loop body 4, execute iteration statement 5, jump to step 2, repeat the execution, note that each statement in the for statement can be empty, the initialization statement is executed only once when the for statement is executed. According to the syntax format of the for statement, the simplest for statement is the following format: For (;); because the loop condition is null, the default value is true, the loop condition is always set, the loop body is the last Semicolon. If such a statement is called an empty statement, the loop is an endless loop and the loop body is an empty statement. In actual code writing, the loop control part is generally written in the parentheses of the for statement, while the loop body only writes the logic-related code. This structure makes the logic very clear. The code for outputting numbers between 0-9 using the for statement is as follows: for (INT I = 0; I <10; I ++) {system. out. println (I) ;}the execution process of the statement is: 1. Execute int I = 0; 2. Judge I <10. If the condition is not true, it ends, otherwise, continue to the next step. 3. Execute system. out. println (I); 4. Execute I ++ 5, jump to step 2 and continue to execute sample code similar to this to calculate the sum of numbers between 1 and. The Code is as follows: int sum = 0; for (INT I = 1; I <= 100; I ++) {sum + = I;} system. out. println (SUM); these are some basic for statements. In general, the for statement and the while statement can achieve simple conversion, for example, the following is a code in the while format written using the for statement: int I = 0; For (; I <10;) {system. O Ut. println (I); I ++;} For more information about the for statement, see the subsequent examples. 5.4.4. SummaryThis section describes the syntax format of the basic loop control statement. During program design, you must understand the syntax format and corresponding features of each statement, in order to use it flexibly according to your own logic in actual use. Like the preceding conditional statements, loop control statements can also be nested to solve complicated logic in actual use, and there is no limit on the hierarchy of nesting in syntax. The while statement and the for statement belong to the structure of "first judge and recycle" in the loop statement, while the do-while statement belongs to the structure of "first loop and then judge". Therefore, from the syntax perspective, the do-while statement can be executed at least once. In actual use, the while statement and for statement can be easily replaced.

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.