Python入门(四) 循环语句

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

    循环控制语句有:

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

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

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


    1. while循环:

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)


你可能感兴趣的:(Python入门(四) 循环语句)