Python之break和continue

for n in range(2, 10):
     for x in range(2, n):
         if n % x == 0:
             #连接符用逗号和加号都可
             print(n, 'equals', x, '*', x)
             break
     else:
         # loop fell through without finding a factor
         print(n, 'is a prime number')
"""
result:
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3

"""



for num in range(2, 10):
     if num % 2 == 0:
         print("Found an even number", num)
         continue
     print("Found a number", num)

"""
result:
Found an even number 2
Found a number 3
Found an even number 4
Found a number 5
Found an even number 6
Found a number 7
Found an even number 8
Found a number 9
"""


Python中的break和continue和C语言是相同的



你可能感兴趣的:(Python学习随笔)