Fully parse the usage of the While LOOP statement in Python and fully parse the python

Source: Internet
Author: User

Fully parse the usage of the While LOOP statement in Python and fully parse the python

In Python programming, the while statement is used to execute a program cyclically. That is, under a certain condition, a program is executed cyclically to process the same task that needs to be processed repeatedly. The basic form is:

While judgment condition: execution statement ......


The execution statement can be a single statement or statement block. The condition can be any expression, and any non-zero or non-null value is true.
When the condition is false, the loop ends.
The execution flow chart is as follows:

Instance:

#!/usr/bin/pythoncount = 0while (count < 9):  print 'The count is:', count  count = count + 1print "Good bye!"

The output result of the above code execution:

The count is: 0The count is: 1The count is: 2The count is: 3The count is: 4The count is: 5The count is: 6The count is: 7The count is: 8Good bye!

When the while statement is run, there are two other important commands: continue, break to skip the loop. continue is used to skip this loop, and break is used to exit the loop, in addition, the "judgment condition" can also be a constant value, indicating that the cycle must be true. The specific usage is as follows:

# Continue and break usage I = 1 while I <10: I + = 1 if I % 2> 0: # Skip output continue print I # output dual number 2, 4, 6, 8, 10i = 1 while 1: # print I # output 1 ~ must be set when the loop condition is 1 ~ 10 I + = 1 if I> 10: # break out of the loop when I is greater than 10

 

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.