While loop and break, continue statement, breakcontinue

Source: Internet
Author: User

While loop and break, continue statement, breakcontinue

The basic loop while: while is used to output continuous statements. It simply prints a series of characters (statements) you want ).

For example, print all numbers from 0 to 100:

1 #-*-coding: UTF-8 = *-2 start = 03 while True: 4 5 print (start) 6 if start = 100:7 break8 # start = start + 19 start + = 1

The preceding simple break statement is used in loops to stop all loops.

For example, print all numbers from 0 to 100 and sum them:

1 #-*-coding: UTF-8-*-2 3 sum = 0 4 start = 1 5 while True: 6 print (start) 7 sum = sum + start 8 if start = 100: 9 break10 start + = 111 print sum

Continue is usually used to end the current loop and does not stop all loops.

For example, print 0, 1, 2, 3, 4, 5, 6, 8, 9, 10:

#-*-Coding: UTF-8 = *-start = 0 while True: if start = 7: start + = 1 continue print (start) if start = 10: break # start = start + 1

 

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.