流程控制,if判断与while循环
1. 流程控制理论
1. 流程控制的定义
流程控制其实是控制事物的执行顺序或者是控制事物的执行
2. 执行流程的分类
1. 顺序结构
流程自上往下运行
目前我们编写的代码都是顺序结构
2. 分支结构
流程会根据某些条件的判断而执行不同的流程
如: if判断

3. 循环结构
程序中需要根据某些条件的反复的做相同的事情,并且会有一个循环的
如: while 循环, for 循环
2.
实例代码 # 1.单if分支 index = 1 if index > 2: print('数字大于2') # 2.if与else分支 index = 1 if index > 2: print('数字大于2) else: print('数字小于等于2') # 3.if与elif与else分支 index = 1 if index >3: print('数字大于3') elif index > 2: print('数字大于2') else: print('数字小于等于2') # 4.if的嵌套 index = 1 if index < 2: print('数字小于2‘) if index <= 1: print('数字小于等于1')
循环结构
1. while循环
语法结构:
while + 条件:
条件成立之后执行的循环体代码
2. while+break循环
while + 条件:
条件成立之后执行的循环体代码
if +条件:
条件成立之后执行的代码(可以有多行)
break

3. while+continue循环
while + 条件:
条件成立之后执行的循环体代码
if +条件:
条件成立之后执行的代码(可以有多行)
continue

代码实例 # 1. while循环 while True: index = 1 print(index) # 2. while + break while True: count = 1 while count < 10: if count ==5: continue count += 1 print(count) # 3. while + continue count = 1 while count < 10: if count == 2: continue count += 1 print(count)
4. Python编程
代码实例
count = 1
while count < 10: # 父代码 当一条代码是判断时,那么他的下一条代码必定是他的子代码 count += 1 # 子代码 两个子代码缩进格式一致 print(count) # 子代码






浙公网安备 33010602011771号