**Section 6 **Flow Control Statement
Hello everyone, the grammatical knowledge we are going to learn today is called "flow control statement".
Looking back on the previous courses, we have learned about constants and variables, operators and expressions about python syntax, and even touched several built-in functions (input() and print()), although we have not yet formally introduced functions concept. Before and after, we have written a lot of python codes, and they can all run, don’t you feel pretty good? !
However, we will always have a question, because so far, our program seems to be able to stay in place, solving some assignment problems, calculation problems, that is, to solve problems within the reach of an expression. Macroscopically, it is still only within the scope of one point, and it "shine" with such a small spark. When we encounter complex problems, such as facing a continuous problem in the business process, how to solve it?
Therefore, our grammatical system, or our knowledge structure, needs to continue to be enriched and improved. In addition to these basic grammatical definitions, it also has the ability to deal with relatively complex problems, which requires the introduction of program flow control statements.
Yes, in this class, we will focus on python:
One, judgment sentence
This figure shows a flow chart of the logic process of the judgment statement (also called an if statement).
The basic grammatical structure of the if statement is as follows:
If condition judgment:
Statement block 1
else:
Statement block 2
**Explanation: **Seeing the structure of this sentence, what can we think of? Anyway, the first thing I thought of was a Chinese grammar structure I learned in elementary school Chinese: if... then... otherwise.... In fact, it originally meant this, and there was nothing profound.
# Example: 6-1
Assign the string entered on the screen to the variable a, and then judge whether it is equal to 3. If it is, it will display "test passed", otherwise, it will display "test end".
a = input('Please enter:')
if a=='3':
print('Test passed!')
else:
print('The test is over!')
There are three characteristics:
Two, branch statement
A variant of the if statement, multiple conditional judgment statements
if condition judgment 1:
Statement block 1
elif condition judgment 2:
Statement block 2
elif condition judgment 3:
Statement block 3
else:
Statement block 4
**Note: **Compared with the previous judgment statement, this grammatical structure actually adds several possibilities, that is, the process of statement execution adds several branches. Its logical thinking and logical relationship are exactly the same.
# Example: 6-2
Assign the string entered on the screen to the variable a, and then judge whether the value of a is equal to 1, 2, 3, if yes, then display "entry level", "ordinary level", "elite level" respectively, otherwise (if both are No) will display "Test End".
a = input ("Please enter:")
if a=='1':
print ("Entry level!")
elif a=='2':
print ("Normal level!")
elif a=='3':
print ("Elite!")
else:
print ("The test is over!")
The characteristics are summarized as follows:
Three, the statement inside the loop
Loop statements allow us to execute a statement or statement group repeatedly.
(1), while loop
While condition judgment:
Statement block 1
(break)
else:
Statement block 2
**Explanation: The execution flow of **while loop statement is like this, when the set condition is established, statement block 1 will be executed repeatedly. Each time it is executed, it will check whether the condition continues to be established? If the condition is not established, the statement following the else will be executed. In statement block 1, we can also set additional conditions for the termination of the loop. If the additional conditions are established, the execution (break) will exit the loop. At this time, statement block 2 will not be executed again.
# Example: 6-3
This is a guessing number game. The screen prompts you to enter a string (which will be assigned to the a variable), and then judge whether the input (a) is not equal to '9'. If it is not equal to, it will continue to remind the user to enter a new string, otherwise, The screen displays "Congratulations! You guessed it."
a = input('Please enter:')
while a! = '9':
print("Please re-enter!")
a = input('Please enter:')
else:
print("Congratulations! You guessed it.")
Summarize the while loop statement, its characteristics are as follows:
(2), for loop
for i_var in sequence:
Statement block 1
else:
Statement block 2
**Explanation: The **for loop will traverse a sequence, starting from bit 0, taking one of the values every time it loops, and then looping through statement block 1 until the end of the sequence, ending the loop, and then executing statement block 2.
We need to pay special attention to:
The for loop and the while loop are fundamentally different in the setting of loop conditions. The condition of the while loop is a true relational expression, that is, a logical judgment. Its result is only true or false, and it determines whether the loop continues. However, the for loop is very special. Its conditional judgment is a "traversal process", that is to say, the for loop always takes a sequence of objects to traverse (check each element that constitutes the sequence) until the end of the traversal (Checking is complete), the cycle can end. Of course, the executable statement block in the loop body can also include the break out condition. If the condition is met, the loop can also be terminated early and jump out. Because of the characteristics of the for loop that traverses sequence objects, we often call it an "iteration statement", and the traversal process has become an "iteration" process.
PS: In addition to string is a sequence type data, we will encounter more sequence type data structures in the future. Sequence type data has three important characteristics: (1) index (ie sequence number, counting from 0), (2) value: the value corresponding to each index sequence number, (3) length: the total number of sequence elements , It is always 1 greater than the maximum index value (because the index is counted from 0). For example "abc", the index of 0 is a, the index of 1 is b, the index of 2 is c, and the length is 3.
**In the following example, check a sequence of strings to see which characters are contained in it, and display it. The following example 6-4 directly displays each character obtained during the iteration process. Example 6-5 traverses the index and loops to display the index of the sequence and the value corresponding to the index. **
# Example: 6-4
a = ”abcde”
for x in a:
print(x)
else:
print("It's over!")
# Example: 6-5
a = "abcde"
for index in range(len(a)):
print(index,a[index])
else:
print("End of index!")
PS: Two built-in functions are used in Example 6-5, explained as follows
(1) len() is a built-in function of python, used to obtain the length of a sequence of objects.
(2) range() is also a built-in function of python, which is used to create a continuous sequence of integers starting from 0, the sequence until the value in front of the specified number.
For example: range(6) will get a number sequence of "0,1,2,3,4,5".
The characteristics are summarized as follows:
summary
In this section, we have learned about three flow control statements (judgment, branch and loop): if, while and for, and their related break and continue statements. These are the most commonly used statements in Python, and the syntax is very simple.
**Special reminders are needed. For students with experience in other languages, these flow control statements of **python have obvious differences compared with other languages (such as C/C++).
In summary, the differences are as follows:
**(1) There is a colon at the end of the main sentence (parent sentence). **
(2) There is no semicolon at the end of all statements.
(3) There are no parentheses for all condition judgments.
Recommended Posts