[英]Why does this code go into infinite while loop?
下面的代碼進入無限循環的時候,其實如果我一步一步做,就應該當值終止i
在這一點變為0 while
條件變為False
。 因此, while
循環應該終止。 根據我的理解,輸出應該是-5 -4 -3 -2 -1 0
因為0
是False
這使得while True
條件為False
。 有人可以解釋為什么代碼不會終止並進入無限循環嗎?
i = -5 # initialization
while True: # condition
print(i) # statement
i += 1
print("exit")
i
的值是什么並不重要; 你的循環只考慮True
的值,它是一個永遠不會變成假的常量。
i = -5
while i != 0: # i alone works, but this is clearer
print(i)
i += 1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.