简体   繁体   中英

How do while loops and if statements work?

i=0
while(i<=20):  # it will print:0,2,4,6,8
    if(i%2==0):
        print(i)
    elif(i==9):
       break
    else:
       i += 1
       continue
    i+=1

First question: if I remove the second 1+=1 ,it will print 0 endlessly, why? What exactly does break / continue do here?

Second question: I learned that when if meets its requirements,it will not keep running to the following elif and else , so when i = 0 , it meets i % 2 = even number , so number 0 will be printed, the rest of following should not run, so how did the number get increased?

  • If you remove i += 1 , at the first iteration, i is 0. It gets to if(i%2==0) . It finds True , so that if block is executed and it skips elif and else . If you remove i+=1 , i will still be 0 for the second iteration, and the same thing will happen, again and again and again...

  • When if meets its requirements, it will skip elif and else , but i+=1 is not in either the elif block or the else block. The if , elif , and else go together, but what is after them gets executed unless a break , continue , return , or exception happens. Your second i+=1 is really not very helpful because the only time none of the things mentioned above happens is when i%2==0 . You can just remove the whole else block because i += 1 will happen without it.

Note first that i starts at 0, and we enter a while loop that keeps running as long as i is less than or equal to 20.

When i equals 9, the condition i%2==0 is false, so it drops to the next elif which evaluates as true. In this case the break keyword means to end the current looping structure (the while ), so the loop ends.

Now imagine that i is an even number (it starts that way), the first if condition evaluates to true causing the value of i to print. It now skips the next elif and the else , going to the bottom of the while ; there i is incremented by 1 (causing it to now be odd). Note that the increment at the bottom is not part of the final else as it is indented less. As it is indented the same as the if/elif/else , it is on the same level and not part of that (it is part of the while ).

When i is odd, the first if evaluates to false, and if it isn't 9, so does the elif , dropping us into the else . Here i is incremented by one (making it even), and the continue means to skip the rest of the loop (the while ) and go back to the beginning (skipping the i+=1 at the bottom).

Thus what we see is

  • i =0, print 0, add 1 to i (from end of while), go back to beginning (reached end of while)
  • i =1, increase it to 2, go back to beginning (skipping rest)
  • i =2, print 2, add 1 to i (from end of while), go back to beginning (reached end of while)
  • i =3, increase it to 4, go back to beginning (skipping rest)
  • i =4, print 4, add 1 to i (from end of while), go back to beginning (reached end of while)
  • i =5, increase it to 6, go back to beginning (skipping rest)
  • i =6, print 6, add 1 to i (from end of while), go back to beginning (reached end of while)
  • i =7, increase it to 8, go back to beginning (skipping rest)
  • i =8, print 8, add 1 to i (from end of while), go back to beginning (reached end of while)
  • i =9, quit

If you remove that final i+=1 , look at what happens when i is 0. The step that changed it from 0 to 1 and allows us to move to the next condition above is that final statement. Thus without that, we can't escape the i =0 condition, and when we go back to the beginning of the while loop, we are still at the same point.

The thing to remember is that your i += 1 is in the scope of your while loop, so at the end of the loop it will always be executed. Thus, if you remove it then you get an infinite loop since no other place are you increasing your i.

Your first if is always hit initially since you initialized i to 0. Thus, unless you hit your second i += 1, you will never change your i.

What continue does, to the best of my knowledge, is skip the rest of the code body to the next iteration of the loop.

If I'm being too confusing, please let me know. Thanks!

For break and continue you should probably read the tutorial :)

In short, but probably not sufficient: continue exits the current run of the enclosing loop (the for ), while the break exits the whole loop .

Your second question has a shorter answer: you have indented blocks after the if/elif/else -- from there, only one gets executed. The code after than (which increases i ) is on the same level with the if , so it will be run afterwards just like after any other statement...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM