while循環語句


While 語句

1.While ---關鍵字 (死循環)
  1. while 條件:

​ 循環體

while True:
    print("123")
    print("456")
    print("789")
    print("999")
    print("666")

​ 知識擴展

  print(bool(5))
  輸出True
  print(bool(-5))
  輸出True
  print(bool(0))
  輸出False
  
  注:數字中非零的都是True
count = 1
while count <= 5:
    print(count)
    count = count + 1
    
 #輸出   12345

count = 5
while count:
    print(count)
    count = count - 1
    
  #輸出   54321
  • break (終止當前循環,break下方的代碼不會執行)
while True:
    print(123)
    print(234)
    break
    print(345)
  #輸出結果  123
			234
  • continue (偽裝成循環體中的最后一行,跳出當前循環,繼續下次循環)
while True:
    print(123)
    print(234)
    continue
    print(345)
print(456)

  #輸出結果 123,234 循環
  1. while else

while else

while+空格+條件+冒號

縮進+循環體

else+冒號

縮進+循環體
while True:
    print(123)
else:
    print(321)
    
# 輸出結果  123
 while False:
     print(123)
 else:
    print(321)
    
# 輸出結果  321
        
總結:
  • 打斷循環的方式(自己修改條件;break)
  • break---打破當前循環
  • continue---跳出當前循環繼續下次循環
  • break和continue相同之處:他們以下的代碼都不執行


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM