How to Use "break" and "continue" in Python "while" Loops

Поділитися
Вставка
  • Опубліковано 25 сер 2024
  • You’ll walk through practical examples of how to use "break" and "continue" in Python when you're dealing with "while" loops. You'll debug the example code, so you'll get a clear sense of what’s happening behind the scenes.
    Click here to learn more: realpython.com...

КОМЕНТАРІ • 72

  • @espedale
    @espedale 3 роки тому +118

    For those who are still confused, 'continue' doesn't really mean continue through the next steps in the loop, it means go back to the beginning of the loop . Why they used 'continue' to represent go back to the beginning is beyond me.

  • @starscanscream3184
    @starscanscream3184 4 роки тому +40

    This was the most STRAIGHTFORWARD explanation. THANK YOU!

  • @lovepankie
    @lovepankie 4 роки тому +10

    You really dont know how much thos tutorial has saved 12 hours of my life. thank you so much. Stay blessed wherever you are

  • @Lostwolf16
    @Lostwolf16 Рік тому +1

    This is best explanation I found on internet, thank you

  • @heulveul4075
    @heulveul4075 3 роки тому +2

    clear as clear can be, thanks for your upload

  • @rodril3550
    @rodril3550 2 роки тому +2

    thanks, before i did not understand this part of my course but now i can use it for my homework :)

  • @dahewang4981
    @dahewang4981 3 місяці тому +1

    Here's my definition: continue means skipping the rest of the code in the current iteration of the loop(ie invalid input), then immediately begins with the next iteration of the same Loop.

  • @goodkarma9759
    @goodkarma9759 3 роки тому +1

    Thanks for this and more importantly thanks for bringing thonny to my attention, it's a game changer

  • @pedestrianrights1257
    @pedestrianrights1257 Рік тому +1

    FAR better explanation than my college professor. THANK YOU.

    • @AZULA69
      @AZULA69 8 місяців тому

      fr bro collage professors sucks

  • @mynameisjeff5971
    @mynameisjeff5971 Рік тому +2

    hello may i know how you show the visualisation of how the code work, 3:13-4:00

  • @dhruv115
    @dhruv115 3 роки тому

    Wow !! Amazing Work !!! Easiest Explanation !!!

  • @baralidova9627
    @baralidova9627 3 роки тому +1

    Thank you, great explanation!

  • @riayaedywidowaty5750
    @riayaedywidowaty5750 2 роки тому

    This is more understandable example. Thanks 😃

  • @PistacchioIceCreamSucks
    @PistacchioIceCreamSucks 2 роки тому

    I FREAKING LOVE YOU!!!!!! THANK YOU FOR THIS VIDEO!!!!

  • @RonaldoAragao-lr8dx
    @RonaldoAragao-lr8dx 11 місяців тому +3

    Hello, what is the name of this IDE you are using to develop in the Python language, I found it very interesting.

  • @rsg4666
    @rsg4666 2 роки тому

    Thank you so much madam

  • @alexandrusterpu
    @alexandrusterpu 5 років тому +12

    Thanks for the tutorial! I'm just curious, what editor/IDE is that?

    • @realpython
      @realpython  5 років тому +13

      It's a beginner-friendly Python IDE called "Thonny": realpython.com/python-thonny/

    • @alexandrusterpu
      @alexandrusterpu 5 років тому +3

      @@realpython Why didn't I know of it until now? Thanks!

    • @yonggangliu5233
      @yonggangliu5233 5 років тому +3

      @@realpython GOOD TOOL

    • @tedbunder2026
      @tedbunder2026 3 роки тому

      Wow seems we are all stuck....

  • @Desertsafariofficial
    @Desertsafariofficial 2 роки тому

    Thank you so much

  • @supersharonnoy6551
    @supersharonnoy6551 3 роки тому

    perfect explanation

  • @nepkin2080
    @nepkin2080 3 роки тому

    holy shit thank you this is actually so useful

  • @labelva4892
    @labelva4892 3 роки тому

    You helped me a lot.Big thanks from me.

  • @rivershields2723
    @rivershields2723 2 роки тому

    Thanks this helped a lot

  • @vanisshen4467
    @vanisshen4467 5 років тому +9

    Was anyone else forbidden to use break and continue in class projects as well?

  • @hersongarcia3157
    @hersongarcia3157 Рік тому

    nice and simple thank you!

  • @sabrinazym
    @sabrinazym 3 роки тому

    great explanation! thank you!

  • @okpalachinemerem1283
    @okpalachinemerem1283 2 роки тому

    Thanks you. This is so apt 👌

  • @animesongs3992
    @animesongs3992 2 роки тому

    Thanks

  • @lforlyrics2260
    @lforlyrics2260 Рік тому

    Are you using sublime text?

  • @nikhilgoyal007
    @nikhilgoyal007 2 роки тому

    Thank you!!

  • @XU0038221
    @XU0038221 3 роки тому

    when you debugged the code line by line, which button did you press? I am wondering...

  • @nisulidayarathna5635
    @nisulidayarathna5635 2 роки тому

    Thankyou ❤️

  • @dileeshaabilash5562
    @dileeshaabilash5562 Рік тому

    what is the IDE she used, by the way ,good tutorial

  • @SergioSovi
    @SergioSovi 4 роки тому

    Which software are you runnign this on?
    Seems really useful for debugging.

  • @numberonetech4990
    @numberonetech4990 2 роки тому

    What kind of interpreter do you use?

  • @neverdie6039
    @neverdie6039 3 роки тому

    Symple code:
    import os
    os.system('pause')

  • @nicolewhite5594
    @nicolewhite5594 2 роки тому

    Can someone explain why there is a 0 before loop is finished if n is supposed to be greater then zero?

  • @geraldtang9498
    @geraldtang9498 3 роки тому

    I made a simple guessing game with two questions, when you finish the first one you head to the second one,so I want that is the user fails the first question the whole game ends even the second question ends. Any ideas??

  • @tinmaned3542
    @tinmaned3542 Рік тому

    Hello, hello! Thanks for the great video! I am confused as to why "0" prints when we implement built in function continue wherein our while loop is set to n > 0. Can someone please explain? Thanks!

    • @abdert8534
      @abdert8534 Рік тому

      It prints 0, because it is during the condition of n=1. When n =1, it is >0, and on the next line it minus itself, so u get the result 0

  • @naveenshokeen7569
    @naveenshokeen7569 3 роки тому

    In which platform you r executing this code?

  • @Stelcio
    @Stelcio 2 роки тому

    Hello great video, could you write me what compiler do you use?

    • @zilpavdighada4673
      @zilpavdighada4673 Рік тому

      It looks like Thonny to me :) There's a cool feature where you can step into each step of the way (helps when debugging) as the program is executed if you click on the tiny insect instead of the play button.

  • @piyushgupta4341
    @piyushgupta4341 3 місяці тому

    IDE name?

  • @huaghbubee4758
    @huaghbubee4758 4 роки тому

    I am trying to make it where once the nested if statement code runs, how do you jump right back into the while loop directly after the nested if statement? what can I do to make it jump out of the if statement once it’s done and continue the while loop? Using your code as an example, when n = 2 I want the if statement to execute, but then jump back into the while loop directly after the if statement and execute the print(n) which would result in “2” being printed. How can I achieve that?

    • @farzanakhan9364
      @farzanakhan9364 9 місяців тому

      In many programming languages, including Python, you can use the continue statement to skip the rest of the code inside a loop and move on to the next iteration of the loop. Here's an example to illustrate how you might use continue in the context of a while loop with a nested if statement:n = 0
      while n < 5:
      n += 1 # Increment n on each iteration
      # Your nested if statement
      if n == 2:
      print("Executing nested if statement for n =", n)
      # Any code you want to execute when n is 2
      # ...
      # Use continue to jump back to the while loop
      continue
      # Code after the if statement that will be executed for all values of n except 2
      print(n) In this example, when n is equal to 2, the nested if statement is executed, and then the continue statement is encountered. This causes the loop to skip the remaining code inside the loop and move on to the next iteration of the while loop. For all other values of n, the print(n) statement after the if statement will be executed.
      When you run this code, you'll see that "2" is printed as a result of the nested if statement, and then the loop continues, printing the values of n from 3 to 5

  • @Qopa
    @Qopa 2 роки тому

    I see. So continue basically skips everything that was previously ran and restart the while loop with the new value.

  • @F_khan1
    @F_khan1 2 роки тому

    name of software used to debug code?

  • @olivia_chen
    @olivia_chen Рік тому

    Why starts from 4

    • @Phymacss
      @Phymacss 10 місяців тому +1

      Cause we did 5 which is the initial n, minus 1. (N-1)

  • @jyde50
    @jyde50 4 роки тому

    is the break under the if statement or the while loop?

  • @Tod357
    @Tod357 2 роки тому

    continue does not work, its not even a command, its just red underlined

  • @TP-cw8fc
    @TP-cw8fc 3 роки тому

    👍

  • @sagnikpoddar6330
    @sagnikpoddar6330 Місяць тому

    1st rule of being coder
    NEVER USE WHITE THEME

  • @ibraheemoguntade2340
    @ibraheemoguntade2340 3 роки тому

    Thanks