Python - While 循环

使用循环 while只要条件为 true.

i = 1
而我<8:
print(i)
i += 1

循环 while 需要变量停止。在我们的例子中,它是变量 i,别忘了递增变量 i 否则循环将无限期运行.

break in while

使用语句 break即使条件为 true.

i = 1
而我<8:
print(i)
if i == 4:
break
i += 1

continue in while

使用语句 continue我们可以跳过当前的迭代,继续下一个迭代。

i = 0
而我<8:
i += 1
if i == 4:
continue
print(i)

else in while

使用语句 else 一旦循环条件 whilefalse.


i = 1
而我< 8:
print(i)
i += 1
else:
print(i 大于 8”)

Commentaires (12)

Connectez-vous pour commenter

Rejoignez la discussion et partagez vos connaissances avec la communauté

JD
Jean Dupont Il y a 2 heures

Excellent tutoriel !

👍 12 Répondre Signaler
CodeurJava ✓ Auteur • Il y a 1 heure

N'hésitez pas si vous avez des questions.