#forandwhile statements.py
#_*_ coding:utf-8 _*_\
#!/usr/bin/python
import sys
print(sys.getdefaultencoding())
import os
print(os.getcwd())
'''迴圈語句
1. 支援for,while和巢狀
2. 任何非零、或非空(null)的值均為true
3. while(true)死迴圈
4. while....else....迴圈執行/不執行(如果是break跳出的迴圈,則不會執行else)
5. for i in range(1, len(lista)-1): #range(設定輸出列表的起始位置)
'''print("******************** 迴圈語句 **********************************")
print("\n" + "***************** while迴圈語句 **********************")
nnum = 0
while nnum < 10:
print("test while: ", nnum)
nnum += 1
print("\n" + "***************** continue 和 break 迴圈語句 **********************")
nnum = 0
while nnum < 20:
nnum += 1
if nnum == 10:
print("test while: ", nnum, " continue")
continue;#continue:跳出此次迴圈,接著執行迴圈
elif nnum == 15:
print("test while: ", nnum, " break")
break;#break:直接退出迴圈
else:
print("test while: ", nnum)
print("\n" + "***************** while(true) 死迴圈語句 **********************")
ncon = 1
while ncon:
strvaluein = input("input int a: ")
ninputa = int(strvaluein)
strvaluein = input("input int b: ")
ninputb = int(strvaluein)
print(ninputa, " + ", ninputb, " = ", ninputa + ninputb)
strvaluein = input("continue?(0/1): ")
ncon = int(strvaluein)
print("\n" + "***************** while...else... 迴圈語句 **********************")
nflag = 3
while(nflag):
print("while: ", nflag)
nflag -= 1
else:
print("while...else: ", nflag)#如果是break跳出的迴圈,則不會執行else
nflag = 5
while(nflag):
print("while: ", nflag)
nflag -= 1
if nflag == 3:
print("break;", nflag)#如果是break跳出的迴圈,則不會執行else
break
else:
print("while...else: ", nflag)
print("******************** if 迴圈語句 **********************************")
for i in
"python":
print("cur ch: ", i)
lista = ["i", 1314, "love", "you"]
for i in lista:
print("cur word: ",i)
print("***************** if(通過序列索引迭代) 迴圈語句 ******************")
lista = ["i", 1314, "love", "you"]
print("lista: ", lista)
print("lista[2]:", lista[2])
for i in range(len(lista)):
print("cur word: ", lista[i])
for i in range(1, len(lista)-1): #range(設定輸出列表的起始位置)
print("cur word: ", lista[i])
print("***************** if....else... 迴圈語句 ******************")
listb = ["i", 1314, "love", "you", 168, "!!!"]
print("listb: ", listb)
for i in range(0, len(listb)): #range(設定輸出列表的起始位置)
if i == 3:
print("if....else...break: ", i);
break; #如果是break跳出的迴圈,則不會執行else
print("cur word: ", listb[i])
else:#如果是break跳出的迴圈,則不會執行else
print("good bye")
print("\n" + "***************** 迴圈巢狀 迴圈語句 ******************")
listb = ["i", "1314", "love", "you", "!!!"]
print("listb: ", listb)
for i in range(0, len(listb)): #range(設定輸出列表的起始位置)
print("word: ", listb[i])
for j in listb[i]:
print("char: ", j)
print("\n" + "******* 輸出2~100的素數(迴圈巢狀) 迴圈語句 ************")
nnum = 2
bflag = false
while(nnum < 100):
ncurnum = 2
bflag = false
while(ncurnum < nnum):
ifnot (nnum % ncurnum):
bflag = true
break;
ncurnum += 1
ifnot bflag:
print(nnum, "是素數")
Python迴圈語句 for迴圈
說明 1 計次迴圈,一般應用在迴圈次數已知的情況下。通常適用於列舉或遍歷序列以及迭代物件中的元素。2 迭代變數用於儲存讀取的值。3 物件為要遍歷或迭代的物件,該物件可以是任何有序的序列物件,如字串 列表 元組等 迴圈體為一組被重複執行的語句。4 for迴圈語句可以迴圈數值 遍歷字串 列表 元組 集合...
Python迴圈語句
while迴圈 1.一般語法 while 控制條件 執行語句 2.迴圈型別 無限迴圈 while true 執行語句 計數迴圈 count 0 while count 10 print count count 1 3.range 內建函式,返回乙個列表 range start,end,step 不包...
Python迴圈語句
python提供了for迴圈和while迴圈,但沒有do.while迴圈。while 判斷條件 執行語句 執行語句可以是單個語句或語句塊。判斷條件可以是任何表示式,任何非零 或非空 null 的值均為true 當判斷條件假false時,迴圈結束。判斷條件 還可以是個常值,表示迴圈必定成立,迴圈將會無...