Record today learning python for and while loops usage for break and continue

Source: Internet
Author: User

There are two main loops for and while in Python, where two different interrupt usages for both loops break with continue.

First look at the following loop code:

1:

1  for  in range: #变量i带入for循环, 0 to 10 cycles through the rang () function 2      if i==7: #插入条件语句if. Continue, when i==7, enter the condition to be judged due to the role of continue exiting the current loop, do not execute the following print (i) (this is the key)3         continue4       Print(i) #打印i

The above loop prints out 0 1 2 3 4 5 6 8 9, the key is No 7, because 7 encounters continue in the loop and exits the current loop directly. Since the For loop itself increases the attribute of the variable value , the exit will continue looping directly from 8 onwards.

2:

1  for  in range:2       if i==7:  #代码与前一个基本相同唯一改变的就是条件语句的continue变成了break.            34       print(i)
As the continue becomes a break, the loop exits the entire loop at 7 (for exiting the entire loop, the variable value loop is no longer added). So the print out is 0 1 2 3 4 5 6.

3:

1x=0# Enter a variable x and assign it a value of 0. 2  while<10: #进入while the <10 cycle. 3    ifX==7# enters the conditional statement to determine if the variable is 7, if it equals 7 because continue exits the current loop.4       Continue5    Print(x) #打印x6X=x+1#x+1 Assign to X later
Note that the while loop and the For loop have an intrinsic difference. The For loop is known for length and can increment the variable value loop itself. But the while can't do it. Increase the value of a variable without x+=1 change the value of the variable to enter a dead cycle of printing 0. When we increase the if...continue this condition when the X variable increases to 7. Exits the current loop because the following code print (i) and x=x+1 are not executed. So X is always 7, and when X is 7, it goes into the while and then executes the continue. So a dead loop is created. The result of printing is 0 1 2 3 4 5 6.

4

1 x=02while <0:3   if x==74      x=x+ 1; 5       Continue 6   Print (x) 7   X+=1
This code adds a x=x+1 to the IF statement on the basis of the above. This way, when the condition is judged, the X is changed from 7 to 8 and then the loop is not affected by the if condition to continue the loop. The final print result is 0 1 2 3 4 5 6 8 9.
Break replaces the continue effect with the For loop. Jump out of the loop directly. The printed result is 0 1 2 3 4 5 6.

Record today learning python for and while loops usage for break and continue

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.