Python while Loop statement

Source: Internet
Author: User

Python while Loop statement

in Python programming, the while statement is used to loop the execution of a program, which, under some conditions, loops through some segments of the program to handle the same tasks that require repeated processing.

the execution statement can be a single statement or a block of statements.

      The judging condition can be any expression, and any value other than 0, or non-null (NULL), is true.

      when the condition false false, the loop ends.

650) this.width=650; "src=" http://www.runoob.com/wp-content/uploads/2013/11/python_while_loop.jpg "alt=" Python_ While_loop "/>

Example :

#! /usr/bin/python

Count = 0

while (Count < 9):

print ' The count is: ', count

Count = Count +1

Print "Good bye!"

The above code executes the output result:

    

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! 

The while statement has two other important commands, Continue,break, to skip loops.

    • The Continue is used to skip the cycle.

    • Break is used to exit the loop, in addition to the "judging condition" can also be a constant value, indicating that the loop must be established.

Here's how:

# Continue and Break usage:

i = 1

While I < 10:

i + = 1

If 1%2 >0: # Skip output when non-even

Continue

Print I # output even 2, 4, 6, 8, ten

i = 1

while = 1: # The loop condition is 1 must be set

Print I

i + = 1

If i >: # When I is greater than 10 o'clock jump out of the loop

Break



This article is from the "HCWJ" blog, make sure to keep this source http://luwenjuan.blog.51cto.com/10967115/1922505

Python while Loop statement

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.