python--Analysis of program control flow

The order of statement execution in a Python program includes three basic control structures: sequence structure, selection structure, and loop structure.

1. Sequence structure

     The basic sequence of execution of statements in a program is executed in the order of the appearance of each statement, which is called sequence structure, see the figure below. Execute statement block 1 first, then execute statement block 2, and finally execute statement block 3. The three are sequential execution relations.

   

2. Choose the structure

      The selection structure can control the execution branch of the code according to the conditions. The selection structure is also called the branch structure. Python uses if statements to implement branch structures.

      The branch structure includes multiple forms: single branch, double branch and multiple branches. The flowchart is as follows:

     

   2.1, single branch structure

            The syntax of the single branch structure of the if statement is as follows:

               if (conditional expression):

                   Statement/block

           When the value of the conditional expression is true (True), the statement after the if is executed, otherwise no operation is performed, and the control will be transferred to the end point of the if statement.

num1 = int(input("Please input a number:"))
num2 = int(input("Please input a number:"))
print(str.format("输入值:{0},{1}", num1, num2))
if(num1 < num2):
    t = num1
    num1 = num2
    num2 = t
print(str.format("降序值:{0},{1}", num1, num2))

 2.2, double branch structure

         The syntax of the double-branch statement structure of the if statement is as follows:

           if (conditional expression):
              statement/statement block 1

          else:

              Statement/statement block 2

       When the value of the conditional expression is true (True), execute statement 1 after if, otherwise execute statement 2 after else

num = int(input("Please input a number:"))
if num >= 0:
    print()
    if num % 2 == 0:
        print("{}是偶数".format(num))
    else:
        print("{}是奇数".format(num))
else:
    print("请输入一个非负数")

2.3, multi-branch structure

        The syntax of the multi-branch structure of the if statement is as follows:     

         if (conditional expression):
              statement/statement block 1

        elif (conditional expression 2):

             Statement/statement block 2

       ...

       elif (conditional expression n):

             Statement/statement block n

      else:

            Statement/statement block n+1

   The function of this statement is to determine which statement to execute according to the value of different conditional expressions

mark = int(input("请输入分数:"))
if(mark >= 90):
    grade = '优'
    print(grade)
elif(mark >= 80):
    grade = '良'
    print(grade)
elif(mark >= 70):
    grade = '中'
    print(grade)
elif(mark >= 60):
    grade = '及格'
    print(grade)
else:
    grade = '不及格'
    print(grade)

      2.4, nesting of if statements

              The structure that contains one or more if statements in an if statement is called nesting of if statements. The general form is as follows:

       

 

3. Loop structure

      The loop structure is used to repeatedly execute one or more statements. Using the loop structure can reduce the workload of repetitive writing of the source program. Many algorithms need to use loop structures. Python uses for statements and while statements to implement loop structures.

      3.1, for loop

              The for statement is used to traverse the elements in a collection of iterable objects and execute a related embedded statement for each element in the collection. When all the elements in the collection are iterated, control is passed to the next statement after the for. The format of the for statement is as follows:

              for variable in object collection:

                   Loop body statement/statement block         

for i in (1, 2, 3):
    print(i)

     3.2, range object

            The Python 3 built-in object range is an iterator object, which generates a sequence of numbers in a specified range during iteration. The format is: range( start, stop[ ,step]) . The numeric series returned by range starts from start and ends at stop (not including stop). If the optional step size step is specified, the sequence grows by step size. E.g:

    3.3, while loop

           Like the for loop, while is also a pre-tested loop, but before the loop starts, the number of repeated executions of the loop statement sequence is not known. The while statement executes the loop statement (block) zero or more times according to different conditions. The format of the while loop statement is: 

           while (conditional expression):

                Loop body statement/statement block

     

count = 0
while (count < 9):
   print("The count is:", count)
   count = count + 1
 
print "Good bye!"

3.4、break

        The break statement is used to terminate the loop statement, that is, if the loop condition does not have a False condition or the sequence has not been completely recursed, it will also stop executing the loop statement. The break statement is used in while and for loops.

        If you use nested loops, the break statement will stop the deepest loop and start executing the next line of code.

       

num = int(input("Please enter a number: "))
flag = True
if num > 1:
    for i in range(2, num//2 + 1):
        if (num % i) == 0:
            flag = False
            break
    if flag:
        print(num, "是质数")
    else:
        print(num, "不是质数")
else:
    print(num, "不是质数")

3.5、continue

        The python continue statement jumps out of this loop, and break jumps out of the entire loop. The continue statement is used to tell python to skip the remaining statements in the current loop, and then continue to the next loop.

        The continue statement is used in while and for loops.

for i in range(1, 6):
    if i == 3:
        continue
    print("i=", i)

 

Guess you like

Origin blog.csdn.net/Light20000309/article/details/112740805