python | control flow statements (a)


A first flowchart of a program

Refers to a program flow chart explaining the basic operation and control flow of the program described by a series of graphics, text and line process, it is the most basic way to program analysis and processes described.

There are 7 a flowchart of the basic elements, wherein the plurality of connection points connected together flowchart, a flow diagram used in larger divided into several parts.


Section 2 of the conditional expression

2.1 Definitions

Also known as a conditional expression "a triplet of expressions" , as long as the value of the conditional expression is not 0, False, null, python are considered equivalent to True, python all the legitimate expression, can be seen as conditional expressions formula.


2.2 Format

There are two conditional expression syntax:

  • Format. 1: X y IF for condition Condition the else , determines the return value is True x, y determines the return value is False
  • Format 2: (x, y) [for condition Condition] , determines the return value is True x, y determines the return value is False
a = eval(input('请输入一个数字'))     # 格式1
1 if a > 0 else 0

b = eval(input('请输入一个数字'))     # 格式2
(0, 1)[b > 0]


Operators 2.3

It can be used in a variety of conditional expression operators, and operators can be cascaded common are:

  • Arithmetic operators: +, -, *, /, //,%, **
  • Relational operators: <,>, <=,> =, ==,! =
  • Test operators: in, not in, is, is not
  • Logical operators: and, or, not (note inert Principle)
  • Bitwise operators: ~, &, |, ^, ","
  • Matrix multiplication operator: @

However, special attention is required, a conditional expression, you can not have the assignment operator=


Section 3 The basic structure of the program

There are three basic structure of the program, "Any program can be a combination of these three basic structure to achieve" :

  • Sequence Structure
  • Branch structure
  • Loop structure


3.1 Sequence Structure

Structure is a sequence of how the program runs in a linear order, performed sequentially, operation procedure shown below:


3.2 branch structure

Branch structure, also known as "selective structure" , the judgment result according to conditions, choose a different operating mode of execution path forward specific branched structure can be subdivided into single branch, double-branch, multi-branch and nested branch.


3.2.1 single branch


Only a single branch if, no else:

  • Range: statement block if the conditional expression is executed when True, it is also called "true range"
  • Execution logic: if the condition is determined to True, the block of statements executed, the converse is not performed

# 单分支结构示例
# 任意输入两个数,按从小到大排列
a = eval(input('请输入第一个数'))
b = eval(input('请输入第二个数'))
if a > b:
    a, b = b, a   # 序列解包,交换两个变量的值
print(a, b)


3.2.2 Dual Branch

There is a single double-branch if, a else:

  • Range: true range (if the interval), false interval (else interval)
  • Execution logic: if judged to True, the true interval performed; if it is determined False, False execution section
  • Abbreviated forms: double branched structure can be abbreviated as: statement block 1 if conditional else block 2 (ternary expressions with similar). determining if condition is True, execute statement blocks 1; the condition is False, The statements 2

# 双分支结构
# 任意输入两个数,按从小到大排列
a = eval(input('请输入第一个数'))
b = eval(input('请输入第二个数'))
if a > b:
    print(b, a)
else:
    print(a, b)

# 双分支结构简写
# 任意输入两个数,按从小到大排列
a = eval(input('请输入第一个数'))
b = eval(input('请输入第二个数'))
print(b, a) if a > b else print(a, b)


3.2.3 Multi-Branch

A plurality of double branches composition on the formation of multi-branch, which has a plurality of if, a else:

  • Range: there are an infinite number of real intervals (if interval), a fake interval (else interval)
  • Execution logic: multi-branch can have an unlimited number of branches elif, but executes a elif, once the end of a branch execution elif, neither all the branches behind the determination is not performed
# 多分支结构
# 将成绩从百分制变换到等级制
socre = eval(input('请输入你的成绩'))
if socre > 100:
    print('成绩必须小于100分,请重新输入')
elif socre >= 90:
    print('A')
elif socre >= 80:
    print('B')
elif socre >= 70:
    print('C')
elif socre >= 60:
    print('D')
elif socre >= 0:
    print('E')
else:
    print('成绩必须大于0分,请重新输入')


3.2.4 nested branch

Nested nested branch also called branched structure, usually by nesting a plurality of different types of branched structure formed. As indicated above, it is a typical nested branch, a total of three layers, a single branch, two branches, multi-branch nested together.

  • 区间:可以有多个真区间(if区间),多个假区间(else区间)

  • 执行逻辑:从外到内,自上而下依次判断执行

# 将成绩从百分制变换到等级制
socre = eval(input('请输入你的成绩'))
if socre > 100 or socre < 0:
    print('输入不正确,请重新输入')
else:
    if socre >= 90:
        print('A')
    elif socre >= 80:
        print('B')
    elif socre >= 70:
        print('C')
    elif socre >= 60:
        print('D')
    else:
        print('E')

Guess you like

Origin www.cnblogs.com/1k-yang/p/12090334.html