Python 程式設計中 while 語句用於循環執行程序,即在某條件下,循環執行某段程序,以處理需要重複處理的相同任務。其基本形式為:
while 判斷條件:
執行語句…
執行語句…
執行語句可以是單一語句或語句區塊。判斷條件可以是任何表達式,任何非零、或非空(null)的值均為true。
當判斷條件假false時,迴圈結束。
執行流程圖如下:

實例:
#!/usr/bin/python
count = 0
while ( count < 9):
print 'The count is:', count
count = count + 1
count = 0
while ( count < 9):
print 'The count is:', count
count = count + 1
#==
執行實例»
The count is: 0
The count is : 1The count is: 2
while 語句時還有另外兩個重要的指令continue,break 來跳過循環,continue 用來跳過該次循環,break 則是用於退出循環,此外"判斷條件"還可以是個常數值,表示循環必定成立,具體用法如下:The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 5
The count is: 6
The count is:
The count is: 8
Good bye!
The count is: 4
The count is: 5
The count is: 6
The count is: 5
The count is: 6
The count is:
The count is: 8
Good bye!
# continue 和 break 用法
i = 1
while i < 10:
i += 1
if i%2 > print i # 輸出雙數2、4、6、8、10
i = 1
while 1:
while 1: += 1
if i > 10: # 當i大於10時跳出迴圈
break
## break
num = raw_input("Enter#1a n num Sharp
循環使用else 語句在python 中,for … else 表示這樣的意思,for 中的語句和普通的沒有區別,else 中的語句會在循環正常執行完(即for 不是透過break 跳出而中斷的)的情況下執行,while … else 也是一樣。
i = 1
while i < 10:
i += 1
if i%2 > print i # 輸出雙數2、4、6、8、10
i = 1
while 1:
while 1: += 1
if i > 10: # 當i大於10時跳出迴圈
break
## break
無限迴圈
如果條件判斷語句永遠為true,迴圈將會無限的執行下去,如下實例:
!/usr/ bin/python
# -*- coding: UTF-8 -*-
var = 1
while var == 1 : # 此條件永遠為true,循環無限執行下
》 # num = raw_input("Enter a number :")
print "You entered: ", num
1 a number :20
You entered: 20
Enter a number :29
You entered: 29
Enter a number :3##betYou 29
Enter a number :3##betYou 29
Enter a number :3##betYou (most recent call last):
File "test.py", line 5, in <module># -*- coding: UTF-8 -*-
var = 1
while var == 1 : # 此條件永遠為true,循環無限執行下
》 # num = raw_input("Enter a number :")
print "You entered: ", num
1 a number :20
You entered: 20
Enter a number :29
You entered: 29
Enter a number :3##betYou 29
Enter a number :3##betYou 29
Enter a number :3##betYou (most recent call last):
##注意:以上的無限循環你可以用 CTRL+C 中斷循環。
循環使用else 語句在python 中,for … else 表示這樣的意思,for 中的語句和普通的沒有區別,else 中的語句會在循環正常執行完(即for 不是透過break 跳出而中斷的)的情況下執行,while … else 也是一樣。
#!/usr/bin/python
count = 0
while count < 5:
print count, " is less than 5"
count = count + 1
else:
print count, " is not less than 5"
#以上實例輸出結果為:count = 0
while count < 5:
print count, " is less than 5"
count = count + 1
else:
print count, " is not less than 5"
# than 5
1 is less than 5#2 is less than 5
類似if語句的語法,如果你的while循環體中只有一條語句,你可以將該語句與while寫在同一行中,
如下:3 is less than 5
4 is less than 5
5 is not lessless is less than 5
5 is not less is .5
##簡單語句組
4 is less than 5
5 is not lessless is less than 5
5 is not less is .5
##簡單語句組
#!/usr/bin/python
flag = 1
while (flag): print 'Given flag is really true!'
print "Good bye!"
注意:
以上的無限迴圈你可以使用 CTRL+C 中斷迴圈。
print "Good bye!"
注意:
以上的無限迴圈你可以使用 CTRL+C 中斷迴圈。
#
相關影片
熱AI工具
Undress AI Tool
免費脫衣圖片
AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。
Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片
Stock Market GPT
人工智慧支援投資研究,做出更明智的決策
熱門文章
如何在Premiere使用AI語音增強? (音訊清理指南)
1 個月前 By 下次还敢
如何將 jQuery 的拖放事件正確遷移到原生 JavaScript
4 週前 By DDD
'EVM”是什麼?以太坊虛擬機器的意義
1 個月前 By DDD
記事本升級、更便宜的 YouTube TV 以及 Nova Launcher 的新主人:新聞綜述
3 週前 By DDD
如何在 PHP 中動態設定嵌套數組的任意深度值
1 個月前 By DDD
熱門話題
# 抖音等級價目表1-75
20516
7
20516
7
# wifi顯示無ip分配
13629
4
13629
4
# 虛擬手機號碼接收驗證碼
11965
4
11965
4
# gmail信箱登陸入口在哪裡
8979
17
8979
17
# windows安全中心怎麼關閉
8504
7
8504
7
熱門工具
記事本++7.3.1
好用且免費的程式碼編輯器
SublimeText3漢化版
中文版,非常好用
禪工作室 13.0.1
強大的PHP整合開發環境
Dreamweaver CS6
視覺化網頁開發工具
SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)












![PHP實戰開發極速入門: PHP快速創建[小型商業論壇]](https://img.php.cn/upload/course/000/000/035/5d27fb58823dc974.jpg)
