python中break和continue的使用例子

# eg1
print('eg2:')
i = 1
while i <= 5:
    if i == 4:
        print('吃饱了,不吃了')
        break
    print(f'吃了第{i}个苹果')
    i += 1

# eg2
print('eg2:')
i = 1
while i <= 5:
    if i == 3:
        print(f'大虫子,第{i}个不吃了')
        # 在continue之前一定要修改计数器,否则会陷入死循环
        i += 1
        continue
    print(f'吃了第{i}个苹果')
    i += 1

你可能感兴趣的:(java,开发语言)