python_learn while 語句

#print('hello')
#input('please input enter')


i = 0 
while i < 10 :
    i += 1
    print(i,'hello')
else:
    print('over')

 

1 hello
2 hello
3 hello
4 hello
5 hello
6 hello
7 hello
8 hello
9 hello
10 hello
over

***Repl Closed***
 

 

# 循環語句
# 循環語句可以使指定的代碼塊重複指定的次數
# 循環語句分成兩種,while循環 和 for循環
# while循環
# 語法:
#   while 條件表達式 :
#       代碼塊
#   else :
#       代碼塊
# 執行流程:
#   while語句在執行時,會先對while後的條件表達式進行求值判斷,
#       如果判斷結果爲True,則執行循環體(代碼塊),
#       循環體執行完畢,繼續對條件表達式進行求值判斷,以此類推,
#       直到判斷結果爲False,則循環終止,如果循環有對應的else,則執行else後的代碼塊

# 條件表達式恆爲True的循環語句,稱爲死循環,它會一直運行,慎用!
# while True :
#     print('hello')

# 循環的三個要件(表達式)
# 初始化表達式,通過初始化表達式初始化一個變量
# i = 0

# # 條件表達式,條件表達式用來設置循環執行的條件
# while i < 10 :
#     print(i)
#     # 更新表達式,修改初始化變量的值
#     i += 1

# 創建一個執行十次的循環
i = 0
while i < 10 :
    i += 1
    print(i,'hello')
else :
    print('else中的代碼塊')

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章