while循环语句


1、while语句会重复执行代码块直到条件不满足

a = 0
while True:
    a += 1
    print(a)

2、用计数器来控制while语句的循环次数

a = 0
while a < 100:
    a += 1
    print(a)

3、使用while循环遍历列表(获取列表中每个元素)

L = [1, 2, 3, 4, 5]
index = 0
while index < len(L):
    print(L[index])
    index += 1
else:
    print('已经获取所有元素')

results matching ""

    No results matching ""