You might still remember, we learned how to design the game of Guess the Number in the two previous classes this week. Well, I ensure you that, this is the last time to mention the game of Guess the Number. Although we have realized multiple times of guessing the number before, this form, does not fit the habit of game. We may expect to exit the program, once we have the correct guess, or to control the progress of game by ourselves. To realize such a mode, it is needed to add the break statement into the loop. Apart from the "break" statement, "continue" and "else" statements are also used in conjunction with the loop. Let's first look at a simple instance of break statement. This instance was actually introduced before. It is about accumulation to repeatedly add values, like "1, 2, 3, 4 …". When will it stop? Here, we see a "break" like this. We might have guessed that, when the accumulated sum is greater than 10, break. What will the break do? In fact, it is to jump out of its current loop structure. The structure in which it is located in this program is just this "while", so we may see that when it jumps out with break, such a statement is then executed. What is the result of executing this program? The initial value is 1 ,plus 2, plus 3. Then, we see 6, plus 4. Has the value become 10? If we add it again, it will be 15. In this position, 15 is greater than 10, so it jumps out of this loop. The result of program execution should be: sumA equals 15, and the value of i is 6, since it has been added with 1. So, here, from this instance, we may see that the effect of a break statement is to terminate the current loop, and then execute the statement after the loop. Let's look at another instance to output the prime numbers between 2 and 100. The prime number is a quite common question. It appears in many questions, like the well-known Goldbach Conjecture. The algorithm of prime number is like this. For a number, like x, if it can be divided, with no remainder, by any number between 2 and the square root of x, with both ends inclusive, it is not a prime number, otherwise, it is a prime number. How can this question be solved? We may use a loop. The outside loop is to control 2 to 100. So, we see that j starts with 2, and j is less than or equal to 100. Do not forget this statement, "j += 1". When writing a program, we may first build the structure, and in the inner loop we can judge whether a particular i is a primer number or not. 2 to the square root of j are represented as i to k here. The two variables start with i, i is less than or euqal to k, and i is accumulated repeatedly. We know its condition for judgment is if j % i == 0, meaning the result value of this number, modulo 2 to the square root of x is 0. That is to say, the value is its factor. So, under this circumstance, is this j not a prime number. Then, at this time, we should use a break statement to jump out of this loop, which is the effect of a break statement. The result of program operation is like this. They are all the primer numbers between 2 and 100. This question can also be solved with the for loop. As for 2 to 100, it may be written like this with for loops. i is between 2 and 100, k equals int (square root i). In the program with while structure just now, we did not use the integer function. We'd better use it. j is between 2 and k. Since for the function of range(), its last value is not included, we must write it as 101 and k+1. If i modulo j equals 0, we'll break. Here comes the critical point of this question. After jumping out of the loop, can we still like in the while structure, use the judgment condition "j > k"? When the condition is true, it will print the value of the prime number i . Think about it for 100 seconds. OK! time's up. We may have a try with the substitution of a number. If i equals 9, when j equals 3, i%j equals 0, execute the break statement to jump out of the loop. What is the value of j then? Has it, as we hoped, become 4 after adding 1? In Python, the for structure with the range() function, does not achieve this effect. In C, the loop variable keeps changing after jumping out of the loop, the value of loop variable will change for one more time. So, in the C language, the for loop is used to solve this question, and we only need to write here "j>k". In Python, a loop variable, does not change after jumping out of the for loop, causing an error in algorithm design. What's more, this program will have another exception. When i equals 2, the value of k is 1. Then, for j in range, it means range (2,2), and j can not correctly acquire the value, leading to the exception of the variable j not defined. Then, what shall we do now? Can we consider it like this, when i modulo j equals 0, we still use the break statement to jump out of the loop, but we simultaneously mark this with a variable. For example, set the variable "flag" to be 0. The initial value of flag is 1. If when jumping out of the loop, flag still has a value of 1 or not 0, then i is a primer number and i will be printed. Next, let's think about it, where shall we put this statement of setting the initial value of flag to be 1? Generally, we may put an initialization statement at the beginning of a program. But, for this question, should the initial flag of each i be 1? So, the statement of "flag = 1" should be put here. Now, this program is OK. In this instance, we may have seen that even for the same structure, different programming languages have their own features. We need to accumulate such knowledge over time. Concerning the judgment of prime number, we'll mention in later sections to solve the question with a user-defined function. We'll find it more natural to solve this question with a function. Sure, the algorithm thought is still the same. Apart from the break statement, the continue statement may also be applied into for and while loops. Let's look at an example. These two examples are both used to print the variable i and repeatedly accumulate the value of i and sumA. We see that the left program, keeps accumulating, and the accumulated value is put into sumA, i starts with 1, 1 plus 2, plus 3. Look, when the addition reaches 3, i equals 3, and it breaks. So, finally, the program should be i=1, sum=1, i=2, sum=3. Let's look at the other program. It uses continue. If i==3, continue. What is the effect of continue? It actually means, when the condition is satisfied, skip the statement after continue. Here, it is the print() function. And, execute the remaining cycles of the loop. So, the effect of continue is to stop the current loop and re-enter the loop. It is different from break. The break is to terminate the entire loop, while continue only stops the current cycle of loop. Let's look at the game of Guess the Number mentioned before. Now, we have "break", so can we achieve such an effect for a correct guess, the system gives a clue and directly exit the program. So, at this position of program, when a correct guess is input, we may directly use the break statement to jump out of the current loop. Besides, we also allow the player to control the times of game. For this purpose, we may use a variable "go" to control it. If the input "go" is "y", meaning yes to continue the program, we may continue to enter the loop, while if the input is not "y", jump out of the loop. Of course, we hope that, to jump out of the loop, a more friendly clue will be given, instead of a blank screen immediately. Here, we use the else statement to print "Goodbye". Do you feel quite strange that there are else statements in while loops? Consider this. That is something special in Python, "else" and "while" may be used together. The effect of the elese statement in the loop is that, if the loop code ends at break, it jumps out of the loop, and does not execute the code in the else statement after it. If the loop ends normally, it will execute the code in "else". Such a structure design is suitable for some problems. A case in point is to judge prime numbers. We judge whehter a "number" is a prime number. We know that if "number" modulo j equals 0, it means that it has a factor/factors within a certain range, and we know it is certainly not a prime number. In this case, it will break to end the loop before the end. The "else" part will no be executed either. Well, suppose it ends normally, meaning it has no factor, is it certainly a prime number? Is it quite natural then to use the "else" statement?