i = 1
total = 0
while i <= 100:
total = total + i
i += 1
print(total)
break语句,在语句块执行过程中终止循环,并且跳出整个循环
i = 1
total = 0
while i <= 5:
if i ==3:
print(f'吃了{i}个苹果,吃饱了')
break
print(f'在吃第{i}个苹果')
i += 1
continue语句,在语句块执行过程中终止循环,并且跳出该次循环,执行下一次循环
i = 1
total = 0
while i <= 5:
if i ==3:
print(f'吃到第{i}个苹果,吃到虫子了,第{i}个不吃了')
i += 1
continue
print(f'在吃第{i}个苹果')
i += 1
while循环嵌套语句
while 条件1:
执行语句
while 条件2:
执行语句
吃三天苹果,每天第三个吃到虫子
j = 1
while j <= 3:
i = 1
total = 0
while i <= 5:
if i == 3:
print(f'吃到第{i}个苹果,吃到虫子了,第{i}个不吃了')
i += 1
continue
print(f'在吃第{i}个苹果')
i += 1
print(f'吃了{j}天了')
j += 1
print('明天不吃了')