Python continue 和 break 的区别

Python continue and break difference

我越来越深入地了解继续的目的。我知道 continue 回到循环的顶部, break 停止执行,而 pass 什么都不做。如果 continue 回到循环的顶部,它不是递归的吗?在我的示例中,continue 用于停止执行。

import re
value = []
items=[x for x in raw_input().split(',')]
for p in items:
    if len(p)<6 or len(p)>12:
        continue
    else:
        pass
    if not re.search("[a-z]",p):
        continue
    elif not re.search("[0-9]",p):
        continue
    elif not re.search("[A-Z]",p):
        continue
    elif not re.search("[$#@]",p):
        continue
    elif re.search("\s",p):
        continue
    else:
        pass
    value.append(p)
print ",".join(value)

continue 就意味着 "don't execute any of the rest of the loop body this time around".

下一次迭代将与您没有 continued 完全相同 – 也就是说,您的循环变量 p 将绑定到 items 的下一个元素和往常一样。

我认为 "recursive" 是指 "won't terminate",因为您怀疑 p 将绑定到 same 元素items 和上次一样。这不是通常的意思 "recursive" – 递归函数通常被理解为显式调用自身的函数,通常使用基本情况来防止无限递归。