Well-off will accompany you to learn JAVA -------- Do-while loop and javawhile loop of the three major Loops
Do... While loops are also used when the number of execution cycles is unknown, while loops and do... The biggest difference between a while loop is that before entering a while loop, the while statement first tests and judges whether the conditions are true or false, and then determines whether to execute the loop body, And do... The while loop is "Let's talk about it first". Each time we execute a loop body first, and then test and judge whether the condition is true or false. Therefore, no matter what the condition of the loop is, use do... When a while loop is executed, at least one loop body is executed. Do... The while LOOP format is as follows:
When the cyclic subject has only one statement, the left and right braces can be removed. When you enter the do... while loop statement for the first time, the loop subject is directly executed regardless of whether the judgment condition (which can be any expression) meets the execution cycle condition. When the execution of the cyclic subject is complete, the test of the judgment condition value starts. If the judgment condition value is true, the cyclic subject is executed again. In this way, the judgment condition and the cyclic subject are tested again, do does not jump off until the value of the condition is false... While loop. Do... While loop execution process:
1. Before entering the do... whle loop, you must first assign the starting value to the loop control variable (or expression.
2. directly execute the cyclic subject. After the cyclic subject is executed, it determines whether to continue executing the Loop Based on the content of the judgment condition. When the condition judgment value is True, it continues to execute the cyclic subject; when the condition judgment value is False, the loop jumps out and other statements are executed.
3. After the statement in the loop body is executed, assign a value (increase or decrease) to the loop control variable (or expression) Again, because do... The content of the loop control variable (or expression) is not automatically changed when the while loop is the same as the while loop... In a while loop, you must assign values to the cyclic control variables by yourself,
Go back to step 2 and re-determine whether to continue the execution cycle.
01 // The following program illustrates the use of the do... while loop
02 public class TestJava3_29
03 {
04 public static void main (String [] args)
05 {
06 int I = 1, sum = 0;
07 // do. while is executed once before judgment. That is, the loop body is executed at least once.
08 do
09 {
10 sum + = I; // accumulate Calculation
11 I ++;
12} while (I <= 10 );
13 System. out. println ("1 + 2 +... + 10 =" + sum); // output the result
14}
15}
Output result:
1 + 2 +... + 10 = 55
First, declare the variable I (Cyclic count and accumulative operand) and sum (sum) to be used in the program, and set sum to 0; because we need to calculate 1 + 2 +... + 10. Therefore, when the first cycle is started, set the I value to 1 and then judge whether I is less than or equal to 10. If I is less than or equal to 10, then, the sum + I value is calculated and then assigned to sum for storage. When the I value does not meet the cycle condition, I will jump out of the loop, indicating that the accumulated operation has been completed, and then the sum value is output, and the program stops running.
Program description:
1. 08th ~ 12 rows using do... While Loop Calculation 1 ~ Increase the number of 10
2. Output 1-13th rows ~ The cumulative result of the number of 10: 1 + 2 +... + 10 = 55 do .. no matter what the condition is, the while loop is done first, so the subject of the Loop will be executed at least once. In daily life, it is not difficult to find do... While loop shadow! For example, before using a cash machine to withdraw money, the user enters the password input screen and asks the user to enter the password three times. If all the passwords are incorrect, the bank card will be swallowed up, the procedure of the program is to use do... The while loop is designed.
A problem with the do-while loop statement in Java
This String answer = ""; well understood, it is the initialization problem. It is a good habit to define a variable and initialize it. In this program, even if you do not initialize it at the beginning, that is, String answer; it is also possible, because the assigned answer = input is carried out in the do statement. next (); so the program is fine, but if you add an output statement System between String answer; and do. out. println (answer); cannot be compiled because variables cannot be referenced before initialization or assignment. For String variables, the value can be null or "" during initialization. null indicates that the string variable does not point to any String and is null; "" indicates that the string variable points to an empty string, which is very important. It points to the string and is just an empty string, so how can you assign values here!
This String answer = ""; is placed in front of do and in the do statement brackets to see what you have to do, if you want to output the statement entered from the keyboard each time in the do statement, regardless of the statement entered before the last press enter, the String answer = ""; whether it is placed in front of do or inside is the same, put it in front, every time the statement in do is executed, the answer value should be assigned again: answer = input. next ();, that is, the string you entered from the keyboard this time. It is the same in do brackets, but it reflects the scope. Similarly, each assignment is answer = input. next (); the difference is that the answer: String answer = "" must be defined every time. After the do operation is complete, the answer variable defined in it will not exist, the next while loop is redefined. If you want to output a String that includes the String that was input since the first time, you must put String answer = ""; before do, and set answer = input. next (); this sentence is changed to answer = answer + input. next!
For do-while loop programs in java
In this loop, I think you have a problem with a. in JAVA, self-contained statements such as a -- and -- a have no difference, are First A-1 before executing the following statement. There is no doubt about the rest.