Talk briefly about the Python process control statement

  • 2020-05-17 05:47:55
  • OfStack

People often say that life is a process of making multiple choices: some people do not have to choose, only 1 way to go; Some people are 1 point better, they can choose 1 from 2; Some people with good abilities or good family background can have more choices. Still have 1 some people in the confused period of life will be in a circle, can not find the direction. For those who believe in god, it is as if god had planned our life course for us in advance. It is also like the hardships set by the immortals in advance for the journey of tang zeng and his disciples. God and the immortals controlled everything. Programming languages can simulate every aspect of human life. Programmers, like gods and immortals, can control the execution of programs through special keywords in the programming language, which constitute flow control statements.

Flow control statements in programming languages fall into the following categories:

Sequence of statements Branch statements Looping statements

The sequential statement does not need a single keyword to control, that is, 1 line of execution, no special instructions. The focus here is on branching and looping.

1. Branch statements

A conditional branching statement is a block of code that determines which branch to execute based on the results of one or more statements (judging conditions) (True/False). The branching statements provided in Python are: if.. else statement, switch.. case statements. if.. The else statement can take the following forms:

Single branch:
if judgment conditions:
The code block
If the code block of a single-branch statement has only one statement, write the if statement and the code on the same line:

if judgment condition: 1 sentence code
Example: determine whether the specified uid is an root user


uid = 0

if uid == 0:
  print("root")

You could also write it like this:


uid = 0

if uid == 0: print("root")

Output result: root

Two branches:

if judgment conditions:
The code block
else:
The code block
Example: print the user id according to user id


uid = 100

if uid == 0:
  print("root")
else:
  print("Common user")

Output: Common user

Many branches:

if judgment condition 1:
Block 1
elif judgment condition 2:
Code block 2
...
elif judgment condition n:
The code block n
else:
Default code block

Example: print letter grades according to student scores


score = 88.8
level = int(score % 10)

if level >= 10:
  print('Level A+')
elif level == 9:
  print('Level A')
elif level == 8:
  print('Level B')
elif level == 7:
  print('Level C')
elif level == 6:
  print('Level D')
else:
  print('Level E')

Output: Level B

Description:

When the expression in the "judgment condition" above can be any expression or an instance of any type of data object. As long as the "true" value of the final return result of the judgment condition is True, it means that the condition is true, and the corresponding code block will be executed; Otherwise, it means that the condition is not valid and we need to judge the next condition.

2. Loop statements

When we need to execute a code statement or block of code multiple times, we can use a loop statement. The loop statements provided in Python are: while loop and for loop. Note that Python does not have do.. while cycle. In addition, there are several loop control statements used to control loop execution: break, continue, and pass.

1. while cycle

Basic form
The basic form of the while loop is as follows:

while judgment conditions:
The code block
Execute the loop body code when the truth test result of the return value of the given judgment condition is True, otherwise exit the loop body.

Example: cyclic printing of the Numbers 0-9


count = 0
while count <= 9:
  print(count, end=' ')
  count += 1

Output: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

while infinite loop

When the judgment condition 1 of while is True, the code in the while loop body will loop forever.

while True:
print(" this is a dead loop ")
Output results:

This is an infinite loop
This is an infinite loop
This is an infinite loop
...
At this point, the operation can be terminated via Ctrl + C.

while..else
Statement form:

while judgment conditions:
The code block
else:
The code block
The code block in else will be executed when the while loop is normally completed. If the while loop is interrupted by break, the code block in else will not be executed.

Example 1: the end of normal execution of the while loop (the statement in else will be executed)


count = 0
while count <=9:
  print(count, end=' ')
  count += 1
else:
  print('end')

The result is: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 end

Example 2: case where the while loop is broken (the statement in else will not be executed)


count = 0
while count <=9:
  print(count, end=' ')
  if count == 5:
    break
  count += 1
else:
  print('end')

Output: 0, 1, 2, 3, 4, 5

2. for cycle

for loops are commonly used to traverse sequences (e.g. list, tuple, range, str), collections (e.g. set), and mapping objects (e.g. dict).

Basic form
The basic format of the for loop:

for temporary variable in iterable object:
The code block
Example: walk through and print 1 element in list


names = ['Tom', 'Peter', 'Jerry', 'Jack']
for name in names:
  print(name)

For sequences, we also iterate through the 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 basic 1, no need to repeat.

3. Loop control statement

The loop control statement can change the execution of the program in the loop body, such as breaking the loop, skipping the loop.

Circular control statement description
break terminates the entire loop
contine skips this loop and executes the next one
The pass pass statement is an empty statement, just to maintain the integrity of the program structure, no special meaning. The pass statement can be used not only in looping statements, but also in branching statements.
Example 1: walk through all the Numbers in the range 0-9 and print out the odd Numbers through a loop control statement


for i in range(10):
  if i % 2 == 0:
    continue
  print(i, end=' ')

Output: 1, 3, 5, 7, 9

Example 2: the first three elements in a list are printed with a circular control statement


uid = 0

if uid == 0: print("root")

0

Output results:

Tom
Peter
Jerry

4. Nested loops

Loop nesting is the embedding of another loop into the body of a loop.

Example 1: the 99 times table is cycled through while


uid = 0

if uid == 0: print("root")

1

Example 2: the 99 times table is cycled through for


uid = 0

if uid == 0: print("root")

2

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 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


Related articles: