avatarLiu Zuo Lin

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

4349

Abstract

ing'</span>, fruit)

<span class="hljs-keyword">if</span> fruit == <span class="hljs-string">'pear'</span>:
    <span class="hljs-built_in">break</span>

<span class="hljs-comment"># eating apple</span> <span class="hljs-comment"># eating orange</span> <span class="hljs-comment"># eating pear</span></pre></div><div id="c575"><pre>fruits = [<span class="hljs-string">'apple'</span>, <span class="hljs-string">'orange'</span>, <span class="hljs-string">'pear'</span>, <span class="hljs-string">'pineapple'</span>, <span class="hljs-string">'banana'</span>]

<span class="hljs-keyword">for</span> fruit <span class="hljs-keyword">in</span> fruits: <span class="hljs-keyword">if</span> fruit == <span class="hljs-string">'pear'</span>: <span class="hljs-built_in">break</span>

<span class="hljs-built_in">print</span>(<span class="hljs-string">'eating'</span>, fruit)

<span class="hljs-comment"># eating apple</span> <span class="hljs-comment"># eating orange</span></pre></div><p id="37d4">In these 2 examples, we switched the <code>print('eating', fruit)</code> and <code>if fruit=='pear': break</code> statements. This matters quite a lot in determining what our program does.</p><p id="75c1">In the top example, the print statement happens <i>before </i>the <code>break</code> statement. Therefore <code>pear</code> prints before the for loop breaks.</p><p id="60c7">In the bottom example, the print statement happens <i>after </i>the <code>break</code> statement. Thus, <code>pear</code> does not print.</p><h1 id="035d">The ‘continue’ keyword</h1><p id="0b11">The <code>continue</code> keyword allows us to skip certain iterations in a loop. In other words, when the <code>continue</code> keyword runs, the current iteration is <i>stopped</i>, and we move on the the next iteration.</p><div id="3c5e"><pre>fruits = [<span class="hljs-string">'apple'</span>, <span class="hljs-string">'orange'</span>, <span class="hljs-string">'pear'</span>, <span class="hljs-string">'pineapple'</span>, <span class="hljs-string">'banana'</span>]

<span class="hljs-keyword">for</span> fruit <span class="hljs-keyword">in</span> fruits: <span class="hljs-keyword">if</span> fruit == <span class="hljs-string">'pear'</span>: <span class="hljs-keyword">continue</span>

<span class="hljs-built_in">print</span>(<span class="hljs-string">'eating'</span>, fruit)

<span class="hljs-comment"># apple</span> <span class="hljs-comment"># orange</span> <span class="hljs-comment"># pineapple</span> <span class="hljs-comment"># banana</span></pre></div><p id="08de">^ here, we <code>continue</code> if fruit equals to ‘pear’. Which means we skip the iteration where fruit equals to ‘pear’. Note that unlike <code>break</code>, the iterations that happen after ‘pear’ still happen.</p><h1 id="fa1c">Order matters for the ‘continue’ keyword</h1><p id="4ecb">Lines inside the loop that happen <i>before </i>the <code>continue</code> keyword will still be run. The <code>continue</code> keyword skips only the lines that happen <i>after </i>it.</p><div id="c9d0"><pre>fruits = [<span class="hljs-string">'apple'</span>, <span class="hljs-string">'orange'</span>, <span class="hljs-string">'pear'</span>, <span class="hljs-string">'pineapple'</span>, <span class="hljs-string">'banana'</span>]

<span class="hljs-keyword">for</span> fruit <span class="hljs-keyword">in</span> fruits: <span class="hljs-keyword">if</span> fruit == <span class="hljs-string">'pear'</span>: <span class="hljs-built_in">continue</span>

<span class="hljs-built_in">print</span>(<span class="hljs-string">'eating'</span>, fruit)

<span class="hljs-comment"># apple</span> <span class="hljs-comment"># orange</span> <span class="hljs-comment"># pineapple</span> <span class="hljs-comment"># banana</span></pre></div><div id="3b1b"><pre>fruits = [<span class="hljs-string">'apple'</span>, <span class="hljs-string">'orange'</span>, <span class="hljs-string">'pear'</span>, <span class="hljs-string">'pineapple'</span>, <span class="hljs-string">'banana'</span>]

<span class="hljs-keyword">for</span> fruit <span class="hljs-keyword">in</span> fruits: <span class="hljs-built_in">print</span>(<span class="hljs-string">'eating'</span>, fruit)

<span class="hljs-keyword">if</span> fruit == <span class="hljs-string">'pear'</span>:

Options

   <span class="hljs-built_in">continue</span>

<span class="hljs-comment"># apple</span> <span class="hljs-comment"># orange</span> <span class="hljs-comment"># pear</span> <span class="hljs-comment"># pineapple</span> <span class="hljs-comment"># banana</span></pre></div><p id="f3cf">In the top example, the <code>print('eating', fruit)</code> line happens <i>after </i>the <code>continue</code> statement. We are thus able to skip the print line when fruit equals to <code>'pear'</code></p><p id="5c1d">In the bottom example, the <code>print('eating', fruit)</code> line happens <i>before </i>the <code>continue</code> statement. Which means that this print line will happen for every single iteration. The <code>continue</code> statement for the iteration where <code>fruit == 'pear'</code> still runs, but it doesn’t skip anything because there is nothing under that line.</p><h1 id="9759">‘break’ and ‘continue’ in nested loops</h1><p id="11a5">Note that both <code>break</code> and <code>continue</code> apply only to the for/while loop that it is directly inside of.</p><div id="121c"><pre><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-string">'abc'</span>: <span class="hljs-keyword">for</span> n <span class="hljs-keyword">in</span> <span class="hljs-string">'123'</span>: <span class="hljs-keyword">if</span> n == <span class="hljs-string">'2'</span>: <span class="hljs-keyword">break</span> <span class="hljs-built_in">print</span>(i, n)

<span class="hljs-comment"># outer loop a</span> <span class="hljs-comment"># inner loop 1</span> <span class="hljs-comment"># outer loop b</span> <span class="hljs-comment"># inner loop 1</span> <span class="hljs-comment"># outer loop c</span> <span class="hljs-comment"># inner loop 1</span></pre></div><p id="d8fc">Here, we have 2 nested for loops — the <code>break</code> statement being in the inner for loop (breaking if <code>n == '2'</code>). Note that the <code>break</code> does not affect the outer for loop <code>for i in 'abc'</code> at all.</p><p id="c209">The <code>continue</code> keyword behaves the same way — it affects only the loop that it is directly in.</p><h1 id="7750">Conclusion</h1><p id="613c">Hope this was clear and helpful</p><h1 id="c7a5">Some Final words</h1><p id="5868"><i>If this story provided value and you wish to show a little support, you could:</i></p><ol><li><i>Clap 50 times for this story (this really, really helps me out)</i></li><li><i>Sign up for a Medium membership using <a href="https://zlliu.medium.com/membership">my link</a> ($5/month to read unlimited Medium stories)</i></li></ol><p id="17ce"><b>My Home Office Setup: <a href="https://zlliu.co/workspace">https://zlliu.co/workspace</a></b></p><p id="807c"><b>My Free Ebooks: <a href="https://zlliu.co/books">https://zlliu.co/books</a></b></p><div id="6360" class="link-block"> <a href="https://zlliu.medium.com/subscribe"> <div> <div> <h2>Get an email whenever Liu Zuo Lin publishes.</h2> <div><h3>Get an email whenever Liu Zuo Lin publishes. By signing up, you will create a Medium account if you don't already have…</h3></div> <div><p>zlliu.medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*VrKFx7hHz9QEsuxv)"></div> </div> </div> </a> </div><h1 id="1963">Level Up Coding</h1><p id="02a2">Thanks for being a part of our community! Before you go:</p><ul><li>👏 Clap for the story and follow the author 👉</li><li>📰 View more content in the <a href="https://levelup.gitconnected.com/?utm_source=pub&amp;utm_medium=post">Level Up Coding publication</a></li><li>💰 Free coding interview course ⇒ <a href="https://skilled.dev/?utm_source=luc&amp;utm_medium=article">View Course</a></li><li>🔔 Follow us: <a href="https://twitter.com/gitconnected">Twitter</a> | <a href="https://www.linkedin.com/company/gitconnected">LinkedIn</a> | <a href="https://newsletter.levelup.dev">Newsletter</a></li></ul><p id="1ba3">🚀👉 <a href="https://jobs.levelup.dev/talent/welcome?referral=true"><b>Join the Level Up talent collective and find an amazing job</b></a></p></article></body>

Break VS Continue VS Pass In Python Loops Explained In 4 Minutes

You’ve probably seen these 3 keywords — break continue and pass — after working with Python for a while (maybe continue to a lesser extent). Here, let’s explore what each of these keywords do in loops, how to use them and when to use them.

The ‘pass’ keyword

Let’s start with the easiest, the pass keyword. The pass keyword literally tells Python to do nothing.

for i in range(5):
    pass    # literally do nothing

We usually use the pass keyword when we haven’t decided what to write inside a function/loop yet, so we just pass so that a SyntaxError won’t be raised. Note that this does not affect our loops in any way

for i in range(5):
    print(i, end=' ')    # print i itself
    pass                 # do nothing, which doesn't affect anything else
    print(i**2)          # print i^2

# 0 0
# 1 1
# 2 4
# 3 9
# 4 16

The ‘break’ keyword

When the break keyword runs, we get out of whatever loop we are in.

for i in range(5):
    print(i)
    
    if i==2:      # break if i equals to 2
        break

# 0
# 1
# 2

^ notice that 3 and 4 are not printed. This is because of the conditional break statement — if i==2 we break out of the for loop, stopping the for loop completely at that point in time.

fruits = ['apple', 'orange', 'pear', 'pineapple', 'banana']

for fruit in fruits:
    print('eating', fruit)

    if fruit == 'pear':
        break

# eating apple
# eating orange
# eating pear

Here, the for loop is forcefully stopped if fruit == 'pear'. Which means that it doesn’t move on to pineapple or banana.

Order matter for the ‘break’ keyword

fruits = ['apple', 'orange', 'pear', 'pineapple', 'banana']

for fruit in fruits:
    print('eating', fruit)

    if fruit == 'pear':
        break

# eating apple
# eating orange
# eating pear
fruits = ['apple', 'orange', 'pear', 'pineapple', 'banana']

for fruit in fruits:
    if fruit == 'pear':
        break

    print('eating', fruit)

# eating apple
# eating orange

In these 2 examples, we switched the print('eating', fruit) and if fruit=='pear': break statements. This matters quite a lot in determining what our program does.

In the top example, the print statement happens before the break statement. Therefore pear prints before the for loop breaks.

In the bottom example, the print statement happens after the break statement. Thus, pear does not print.

The ‘continue’ keyword

The continue keyword allows us to skip certain iterations in a loop. In other words, when the continue keyword runs, the current iteration is stopped, and we move on the the next iteration.

fruits = ['apple', 'orange', 'pear', 'pineapple', 'banana']

for fruit in fruits:
    if fruit == 'pear':
        continue

    print('eating', fruit)

# apple
# orange
# pineapple
# banana

^ here, we continue if fruit equals to ‘pear’. Which means we skip the iteration where fruit equals to ‘pear’. Note that unlike break, the iterations that happen after ‘pear’ still happen.

Order matters for the ‘continue’ keyword

Lines inside the loop that happen before the continue keyword will still be run. The continue keyword skips only the lines that happen after it.

fruits = ['apple', 'orange', 'pear', 'pineapple', 'banana']

for fruit in fruits:
    if fruit == 'pear':
        continue

    print('eating', fruit)

# apple
# orange
# pineapple
# banana
fruits = ['apple', 'orange', 'pear', 'pineapple', 'banana']

for fruit in fruits:
    print('eating', fruit)

    if fruit == 'pear':
        continue

# apple
# orange
# pear
# pineapple
# banana

In the top example, the print('eating', fruit) line happens after the continue statement. We are thus able to skip the print line when fruit equals to 'pear'

In the bottom example, the print('eating', fruit) line happens before the continue statement. Which means that this print line will happen for every single iteration. The continue statement for the iteration where fruit == 'pear' still runs, but it doesn’t skip anything because there is nothing under that line.

‘break’ and ‘continue’ in nested loops

Note that both break and continue apply only to the for/while loop that it is directly inside of.

for i in 'abc':
    for n in '123':
        if n == '2':
            break
        print(i, n)

# outer loop a
# inner loop 1
# outer loop b
# inner loop 1
# outer loop c
# inner loop 1

Here, we have 2 nested for loops — the break statement being in the inner for loop (breaking if n == '2'). Note that the break does not affect the outer for loop for i in 'abc' at all.

The continue keyword behaves the same way — it affects only the loop that it is directly in.

Conclusion

Hope this was clear and helpful

Some Final words

If this story provided value and you wish to show a little support, you could:

  1. Clap 50 times for this story (this really, really helps me out)
  2. Sign up for a Medium membership using my link ($5/month to read unlimited Medium stories)

My Home Office Setup: https://zlliu.co/workspace

My Free Ebooks: https://zlliu.co/books

Level Up Coding

Thanks for being a part of our community! Before you go:

🚀👉 Join the Level Up talent collective and find an amazing job

Python
Python Programming
Programming
Coding
Recommended from ReadMedium