Java programming those things 36-do-while statement syntax Zhengzhou game college Chen yuefeng from: http://blog.csdn.net/mailbomb
5.4.2 do-while statementThe do-while statement is composed of the keywords do and while. It is the most typical process control structure of "loop first and then judge" in a loop statement. This is different from the other two loop statements. The syntax format of the do-while statement is do {loop body;} while (loop condition). syntax description: In the do-while statement, the part of the loop body is the part of the code that is repeatedly executed, and the condition that the loop is established requires that the loop condition is of the boolean type. If the value is true, the loop is executed cyclically. Otherwise, the loop ends, the entire statement ends with a semicolon. Execution Process: When the do-while statement is executed, the loop body is first executed, and then the loop condition is judged. If the loop condition is not true, the loop ends. If the loop condition is true, then, execute the loop body. After the loop body is executed, judge the loop conditions, and so on. The sample code for implementing an endless loop is do {system. out. println ('A');} while (true); the loop for outputting the 10 numbers 0-9 is: int I = 0; do {system. out. println (I); // output variable value I ++; // variable increase by 1} while (I <10 ); the code for calculating the sum of the 10 numbers 1-10 is: int I = 1; int sum = 0; do {sum + = I; // sum I ++; // Add 1} while (I <10); system. out. println (SUM); // The output is similar to the code used to calculate the factorial of 5. in mathematics, the factorial of 5 is 1 × 2 × 3 × 4 × 5, in mathematics, it is specified that the factorial of 0 is equal to 1. In actual calculation, the factorial value increases very quickly, so note that the result cannot overflow. The specific code is: int I = 1; int result = 1; do {result * = I; I ++;} while (I <= 5); system. out. println (result); in the actual program, the advantage of do-while is to implement the logic that first loops and then judges, which can reduce code duplication to a certain extent, however, the do-while statement is generally used more frequently than other loop statements.