Python program flow control statement: if branch structure


There are three kinds of program structure: sequential structure, branch structure and loop structure. Statements in a program are executed in sequence called sequential structure. Branching structures execute different code based on conditions. A loop structure executes the same code repeatedly. Python implements branching structures with if statements and looping structures with for and while statements.

if branch structure

Python uses the if conditional statement to implement the branching structure of the program, including single-branch, double-branch and multi-branch structure.

single branch structure

The single-branch structure is composed of an if statement. When the running result of the conditional expression is True, the code in the statement block is executed; when the running result of the conditional expression is False, the code in the statement block is not executed and will be executed. The code after the if condition, as shown in the flowchart:
insert image description here

The syntax is as follows:

if 条件表达式:
	语句块

An example is as follows:

# 判断是否是正数
>>> x = eval(input('请输入一个数:'))
>>> if x > 0: 
...		print('您输入的是一个正数') # 满足if条件时执行该语句
>>> print('程序执行完毕') # 无论if条件是否满足都是执行

# 当输入3时:
  您输入的是一个正数
  程序执行完毕
# 当输入-3时:
  程序执行完毕

double branch structure

The double-branch structure is composed of if...else...statements. When the running result of the if conditional expression is True, the code in the statement block 1 is executed; otherwise, the code in the code block 2 under the else is executed, as shown in the flowchart:
insert image description here

The syntax is as follows:

if 条件表达式:
	语句块1
else:
	语句块2

An example is as follows:

>>> x = eval(input('请输入一个数:'))
>>> if x > 0: 
...		print('您输入的是一个正数') # 满足if条件时执行该语句
>>> else:
...		print('您输入的是一个负数或者零') # 否则,执行该语句

# 当输入3时:
  您输入的是一个正数
# 当输入-3时:
  您输入的是一个负数或者零

multi-branch structure

The multi-branch structure is composed of if...elif...else... statements, and the else part can be omitted. The multi-branch structure runs in sequence. If the result of the if conditional expression is True, execute statement block 1. If it is False, continue to judge the result of the next elif conditional expression. If the result of the conditional expression is True, execute The corresponding statement block, if it is False, continue to judge the following conditions. If all conditional expressions result in False, the statement block in the else part is executed (the statement block in the else part exists). The flow chart is shown in the figure:
insert image description here

The syntax is as follows:

if 条件表达式1:
	语句块1
elif 条件表达式2:
	语句块2
	
......

elif 条件表达式n:
	语句块n
else:
	语句块n+1

An example is as follows:

>>> score = eval(input("请输入考试成绩: "))
>>> if score >= 90:
>>>     print("优秀")
>>> elif score >= 80:
>>>     print("良")
>>> elif score >= 70:
>>>     print("中等")
>>> elif score >= 60:
>>>     print("一般")
>>> else:
>>> 	print('差')

# 当输入85时

Ternary expression

A ternary expression is a simplified version of an if...else... statement with the following syntax:

表达式1 if 条件表达式 else 表达式2

When the result of the conditional expression is True, the value of expression1 is used as the result of the ternary expression; otherwise, the value of expression2 is used as the result of the ternary expression. An example is as follows:

>>> a = 5
>>> b = 10
>>> x = a if  a > b else b # 满足a>b的条件则返回a的值,否则返回b的值
>>> print(x)
10

truth test

Python differs from C and C++ in the way it handles truth values ​​and logical operations. In python:

  • True for any non-zero number and non-null object.
  • Number 0, empty object (such as empty list [], empty dictionary {}), None are all False.
  • Comparison operations return True or False.
  • The logical operations and and or return the true or false objects involved in the operation.
  • Short-circuit operations (a special case of logical operations)

Guess you like

Origin blog.csdn.net/shield911/article/details/124182882