People often say that life is a process of making multiple-choice choices: Some people have no choice but one way to go; some people are better, they can choose one, some are good or the family is good, there are more choices, and some people in the life of confusion will be in situ spinning, can not find the direction. For those who believe in God, it is as if God had set the course for us in advance, and it seems that the gods and the gods have mastered all the hardships that have been set up in advance of the path of the scriptures. Programming languages can simulate all aspects of human life, and programmers are like gods and immortals. Through the process of the Special Keyword Control program in programming language, these keywords consist of Process Control statements.
The Process Control statements in a programming language are grouped into the following categories:
- Sequential statements
- Spoke statements
- Loop statement
The sequential statement does not require a separate keyword to control, is the execution of a line of rows, and does not require a special description. The main point here is the branch statements and circular statements.
One, branch statements
A conditional branch statement is a block of code that determines which branch is executed through the execution result (true/false) of one or more statements (judging criteria). The branch statements provided in Python are: if.. Else statement, no switch is provided ... Case statement. If.. The Else statement has the following several forms:
Single branch:
If Judge condition:
code block
If the code block of a single branch statement has only one statement, you can write the if statement and the code on the same line:
If judgment condition: a sentence code
Instance: Determines whether the specified UID is a root user
UID = 0
If uid = 0:
print ("root")
You can also write this:
UID = 0
if uid = = 0:print ("root")
Output Result: Root
Double branch:
If Judge condition:
code block
Else
code block
Instance: Print user identity based on user ID
UID =
If uid = 0:
print ("root")
else:
print ("Common user")
Output Result: Common user
Multiple branches:
If Judge condition 1:
Code Block 1
Elif Judgment Condition 2:
Code Block 2
...
Elif Judge Condition N:
Code block n
Else
Default code block
Example: Print letter grades based on student scores
Score = 88.8 Level
= Int (score%)
if level >=:
print (' Level + ') elif level
= 9:
print (' Leve L A ')
elif level = 8:
print (' Level B ')
elif level = 7:
print (' Level C ')
elif level = 6:
Print (' Level D ')
else:
print (' Level E ')
Output Result: Level B
Description
The expression in the "judgment condition" above can be any expression, or it can be an instance of any type of data object. As long as the "true" value of the final return result of the judgment condition is tested to true, it means that the condition is established and the corresponding code block is executed; otherwise, the condition is not tenable and the next condition needs to be judged.
Second, the circular statement
Loop statements can be used when we need to execute a code statement or block of code more than once. The loop statements provided in Python are: while loops and for loops. Be aware that there is no do in Python. While loop. In addition, there are several circular control statements that control the loop execution process: Break, continue, and pass.
1. While loop
Basic form
The basic form of a while loop statement is as follows:
While to determine the condition:
code block
Executes the code of the loop body when the value of the return of the given judgment condition evaluates to true, otherwise exits the loop body.
Instance: Looping print number 0-9
Count = 0 While
count <= 9:
print (count, end= ')
count = 1
Output results: 0 1 2 3 4 5 6 7 8 9
While Dead loop
When the while judgment condition is true, the code in the while loop body loops forever.
While True:
Print ("This is a dead loop")
Output results:
It's a dead loop.
It's a dead loop.
It's a dead loop.
...
You can terminate the operation by Ctrl + C at this time.
While.. Else
Statement form:
While to determine the condition:
code block
Else
code block
The code block in else is executed when the while loop is performing normally, and if the while loop is interrupted by a break, the code block in else is not executed.
Instance 1:while the end of the normal execution of the loop (the statement in else will be executed)
Count = 0
while count <=9:
print (count, end= ')
count + + 1
else:
print (' End ')
The results are: 0 1 2 3 4 5 6 7 8-9 End
Instance 2:while the loop is interrupted (the statement in else will not be executed)
Count = 0
while count <=9:
print (count, end= ")
If Count = 5: Break
count + + 1
else:
pri NT (' End ')
Output results: 0 1 2 3 4 5
2. For loop
A For loop is typically used to traverse a sequence (such as list, tuple, range, str), a collection (such as set), and a mapping object (such as Dict).
Basic form
The basic format for the For loop:
For temporary variable in can iterate objects:
code block
Instance: Traversal prints an element in a list
names = [' Tom ', ' Peter ', ' Jerry ', ' Jack '] for
name in names:
print (name)
For sequences, they are also iterated by index:
names = [' Tom ', ' Peter ', ' Jerry ', ' Jack '] for
i in range (len (names)):
print (Names[i])
Execution results:
Tom
Peter
Jerry
Jack
For...else
with while. else is basically consistent, no longer repeat.
3. Loop Control statement
A circular control statement can change the execution of a program in a loop body, such as interrupting loops and skipping this cycle.
Circular Control Statement Description
Break terminates the entire loop
Contine skip this loop and perform the Next loop
Pass Pass statement is an empty statement, just to maintain the integrity of the program structure, there is no special meaning. The pass statement is not available only in circular statements, but also in branch statements.
Example 1: Iterate through all the numbers in the 0-9 range and print out the odd number in the loop control statement
For I in range:
if i% 2 = 0:
continue
print (I, end= ')
Output results: 1 3 5 7 9
Example 2: Print the first 3 elements of a list through a circular control statement
names = [' Tom ', ' Peter ', ' Jerry ', ' Jack ', ' Lilly '] for
i in range (len (names)):
If I >= 3:
break
print ( Names[i])
Output results:
Tom
Peter
Jerry
4. Loop nesting
Loop nesting refers to the embedding of another loop inside a loop body.
Example 1: Printing a 99 multiplication table through a while loop
j = 1
while J <= 9:
i = 1
while I <= J:
print ('%d*%d=%d '% (i, J, i*j), end= ' t ')
i + = 1
pri NT ()
J + + 1
Example 2: Print a 99 multiplication table through a for loop
For j in Range (1):
for I in range (1, j+1):
print ('%d*%d=%d '% (i, J, i*j), end= ' \ t ')
i = 1
print ( )
j = 1
Output results:
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15& nbsp 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1 *7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2 *8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81