0%

Python | 循环 Loop

for循环

  • if...elif...else的多个语句块中只会执行一个语句块
  • forwhile都可以有else语句

for…else

  • for循环正常执行完的情况下,执行else输出
  • for循环中执行了跳出循环的语句(如break),将不执行else代码块的内容
1
2
3
4
for condition:
statement1
else:
statement2

while循环

while…else

  • while循环正常执行完的情况下,执行else输出
  • while循环中执行了跳出循环的语句(如break),将不执行else代码块的内容
1
2
3
4
while condition:
statement1
else:
statement2
1

break语句

  • break语句用于终止当前循环
  • 通常与ifif...elseif...elif...else语句一起使用

continue语句

  • continue语句用于跳过当前剩余要执行的代码,执行下一次循环
  • 通常与ifif...elseif...elif...else语句一起使用

pass语句

  • pass不做任何事情,一般用作占位语句

参考资料

Thank you for your approval.

欢迎关注我的其它发布渠道