擅长:python、mysql、java
<p>使用<code>break</code>和<code>continue</code>执行此操作。在Python中,可以使用以下方法断开嵌套循环:</p>
<pre><code>for a in range(...):
for b in range(..):
if some condition:
# break the inner loop
break
else:
# will be called if the previous loop did not end with a `break`
continue
# but here we end up right after breaking the inner loop, so we can
# simply break the outer loop as well
break
</code></pre>
<p>另一种方法是将所有内容包装在函数中,并使用<code>return</code>从循环中转义。</p>