#break语句
break语句是导致从循环体任意位置退出的语句。当语句break在一个while循环体中执行时,循环会马上终止。break语句通常出现在if语句里面。
下面这个程序就是一个利用break语句来获取数字序列的例子。
list1=[]
print("(Enter -1 to terminate entering numbers.)")
while True:
num=eval(input("Enter a nonnegative number:"))
if num==-1:
break
list1.append(num)
#continue语句
continue语句与上面说到的break语句一样具有终止循环的作用,但它们在程序中起到的作用却不相同。当continue语句在一个循环体中执行时,当前循环会终止,程序跳转到循环的头部。continue语句通常出现在if语句的里面。
下面的程序检测一个列表中可以被11整除的第一个整数。
list1={"one",23,17.5,"two",33,22.1,242,"three"}
i=0
foundFlag=False
while i<len(list1)
x=list1[i]
i+=1
if not isinstance(x,int):
continue
if x%11==0:
foundFlag=True
print(x,"is the first int that is divisible by 11.")
break
if not foundFlag:
print("There is no int in the list that is divisible by 11.")