Program control three major processes
1. Sequence:
The code is from top to bottom, from left to right
2. Choice:
Single branch
> if 条件:
> # 条件成立时要执行的代码
if (i + 3) % 5 == 0 and (i - 3) % 6 == 0:
print("这个最小数是:",i)
Double branch
if 条件:
# 条件成立时要执行的代码
else:
# 条件不成立时要执行的代码
if x % 4 == 0 and x % 100 != 0 or x % 400 == 0:
print("该年份为闰年")
else:
print("该年份不是闰年")
Multi-branch
if 条件1:
# 条件1成立时要执行的代码
elif 条件2:
# 条件2成立时要执行的代码
…
else:
# 前面条件都不满足时要执行的代码
if BMI < 18.5:
print("过轻")
elif BMI >= 18.5 and BMI < 24:
print("正常")
elif BMI >= 24 and BMI < 27:
print("过重")
elif BMI >= 27 and BMI < 30:
print("轻度肥胖")
elif BMI >= 30 and BMI < 35:
print("中度肥胖")
elif BMI >= 35:
print("重度肥胖")
else:
print("输入错误!")
3. Loop:
What is a loop:
a statement structure for repeated code execution
Types of loops:
while loop
for loop
while loop: (note that the semicolon is in English format)
while condition:
# loop body
while i <= layer:
Use of break and continue keywords:
break——The function of terminating the loop
continue——Skip this loop and enter the next loop
else——Only execute when the program loop is running normally
If the loop is broken, it will not terminate
Infinite loop: the condition is always established and executed forever