天天看点

continue

continue
continue

 使用break的时候,循环如下:

for i in range(1,10):
	for j in range(2,4):
		if i % j == 0:
			break
		else:
			print(i,j)

			
1 2
1 3
3 2
5 2
5 3
7 2
7 3
9 2
      

  使用continue 的时候,循环如下:

for i in range(1,10):
	for j in range(2,4):
		if i % j == 0:
			continue 
		else:
			print(i,j)

			
1 2
1 3
2 3
3 2
4 3
5 2
5 3
7 2
7 3
8 3
9 2
      

  continue 语句是一个删除的效果,他的存在是为了删除满足循环条件下的某些不需要的成分:

continue
continue
continue
x=10
>>> while x:
	x=x-1
	if x %2 !=0:
		continue
	print(x)

	
8
6
4
2
0
      
>>> x=12
>>> while x:
	x=x-1
	if x %2 !=0:
		continue
	else:
		print(x)

		
10
8
6
4
2
0