1.pass
过 占位作用,防止没有内容而代码报错,先用pass占位,后期可将代码补进来
if 10==10:
pass
if 10==10:
pass
2.break
终止'当前'循环
(1)打印1~10,遇到5就终止
i=1
while i<=10:
if i==5:
break
print(i)
i+=1
(2)仅仅终止当前循环
i=1
while i<=3:
j=1
while j<=3:
if j==2:
break
print(i,j) #1,1 2,1 3,1
j+=1
i+=1
3.continue
(1)打印1~10,遇到5就终止
i=1
while i<=10:
if i==5:
break
print(i)
i+=1
(2)仅仅终止当前循环
i=1
while i<=3:
j=1
while j<=3:
if j==2:
break
print(i,j) #1,1 2,1 3,1
j+=1
i+=1
3.continue
跳过当前循环,从下一次循环开始(当执行continue时,continue后面循环中的代码不执行,直接返回while语句进行下一次循环)
(1)打印1~10,跳过5
i=1
while i<=10:
if i==5:
i+=1 #需要手动增加出现continue时的自增,否则会出现死循环
continue
print(i)
i+=1
(2)打印1~100所有不含4的数
方法一
i=1
while i<=100:
if i//10==4 or i%10==4:
i+=1
continue
print(i)
i+=1
方法二
i=1
while i<=100:
strvar=str(i)
if '4' in strvar:
i+=1
continue
print(i)
i+=1
(1)打印1~10,跳过5
i=1
while i<=10:
if i==5:
i+=1 #需要手动增加出现continue时的自增,否则会出现死循环
continue
print(i)
i+=1
(2)打印1~100所有不含4的数
方法一
i=1
while i<=100:
if i//10==4 or i%10==4:
i+=1
continue
print(i)
i+=1
方法二
i=1
while i<=100:
strvar=str(i)
if '4' in strvar:
i+=1
continue
print(i)
i+=1