Python入门(四) 循环语句

    Python中提供了while循环与for循环,注意没有提供do...while循环。
python

    循环控制语句有:
app

1)break:终止当前的循环,并退出整个循环体;oop

2)continue: 终止当前循环,跳出本次循环,执行下一次循环;ui

3)pass:空语句,是为了保持程序结构的完整性;spa


    1. while循环:
code

while 判断条件:
    执行语句...
count = 0
while (count < 9):
    print("count = %d“ % count)
    count++
    
print("Exit")
# 无线循环
while True:
    print("while loop")

    2. for循环: 能够遍历任何序列的项目,如一个列表或者一个字符串。字符串

for iterating_var in sequence:
    statements(s)
for letter in "hello world":
    print("current letter: ", letter)

for i in range(10):
    print(i)
    
fruits = ['apple', 'banana', 'orange']
for fruit in fruits:
    print(fruit)
相关文章
相关标签/搜索