Java programming 35-while statement syntax

Source: Internet
Author: User

Java programming those things 35 -- while statement syntax Zhengzhou game college Chen yuefeng from: http://blog.csdn.net/mailbomb 5.4 loop statementsCyclic statements are used in programming to describe the process with repeated rules. In actual programs, there are many processes that need to be repeated. to simplify these Repetitive execution processes, this type of statements is added to the programming language. When learning a loop statement, the most important thing is to discover the rule of the process, and then use the programming language to describe the rule, and always implement the process required by the program. Loop statements are the most complex, useful, and difficult statements in Process Control. At first, you must familiarize yourself with the basic syntax and then quickly observe the rules of the process, this observation capability relies on a large number of reading and writing programs for training. This is the basic logic thinking, and then we can describe this rule. Therefore, when learning a loop statement, learning the syntax is only the basic content. It is more about cultivating the ability to observe the law. This is the difficulty and focus of learning a loop statement. This section describes three syntax formats of loop statements: While statements, do-while statements, and for statements. 5.4.1 while statementThe Chinese meaning of the while keyword is "when ...... When the condition is set, the corresponding code is executed cyclically. The while statement is a basic structure in a loop statement. The syntax format is relatively simple. Syntax format of the while statement: While (loop condition) indicates the loop body. In order to have a clear structure and make the loop body part write multiple lines of code, the loop body is generally processed as a code block, the syntax format is changed to: While (loop condition) {loop body;} syntax description: similar to the IF statement, if the structure of the code block is not used, then, the first statement after "while" is a loop body statement. In this syntax, the type of the cyclic condition must be boolean, which refers to the condition for loop establishment. The specific part of the loop body is the code that needs to be repeated. Execution Process: when executing the while statement, first judge the loop condition. If the loop condition is false, directly execute the subsequent code of the while statement. If the loop condition is true, then execute the loop body code, and then judge the loop condition until the loop condition is invalid. The following uses a specific example to demonstrate the basic usage of the while statement. First, let's implement an infinite loop, also known as an infinite loop. The specific code is as follows: While (true) {system. out. println ('A');} The following describes the execution sequence of the while statement. First, judge the loop condition of the while statement. If the condition is true, the code of the loop body is executed, and the output character, then judge the loop condition, the condition is true, continue to execute the loop body code, output A, and then judge the loop condition ......, And so on, because the loop condition is always true, so the function of the program is always Output A, never stop. Note: Execute an endless loop program on the console. You can press Ctrl + C to end the loop and run the endless loop program in eclipse, you can select the red button "Terminate" in the execution window to end the program. The following uses the while statement to output the 10 numbers 0-9. The principle implemented by the program is to use a variable to represent the numbers between 0-9 and output the value of this variable each time, add 1 to the value of this variable each time. The variable value starts from 0 and runs the loop as long as it is smaller than the number 10. The specific sample code is as follows: int I = 0; while (I <10) {system. out. println (I); // output variable value I ++; // variable value increases by 1} the execution process is: 1. Execute int I = 0; 2. judge whether I <10 is true. If not, it ends, otherwise, execute step 3, output variable I value 4, increase the value of I by 15, jump to step 2, continue to execute, note that the first condition in the while statement is the condition that the loop is established, that is, when this condition is set up, the cycle continues, so pay attention to it during writing. In addition, the internal code is written in an order similar to the above Code. If you adjust the internal code order, it is shown as follows: int I = 0; while (I <10) {I ++; // The variable value is increased by 1 system. out. println (I); // output variable value} Then the execution result of the program is changed to the output number 1-10. Therefore, in loop statements, the order between codes also affects the logic of the entire program. The following describes how to use a loop to implement a simple mathematical logic and calculate the sum of the 10 numbers 1-10. The principle of the program is as follows: declare a variable I, from 1 to 10, assign a value to itself after declaring a variable sum and adding the value of I each time, the next time sum is the sum of I after the change. After the cycle ends, the result is the sum of all numbers between 1 and 10. The sample code is as follows: int I = 1; // The cyclic variable int sum = 0; // number and while (I <= 10) {sum + = I; // Add I ++ to the current I value; // variable I is increased by 1}. In this way, the first loop assigns the result of adding 1 to sum, then, use the sum value and the sum of 2 and assign the value to sum, and so on. Then, the sum of all the variables between 1 and 10 is obtained. When using cyclic statements, it takes much more time to discover patterns than to write and debug code. Therefore, you must be good at discovering patterns and thinking.

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.