天天看點

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