Python while loop

Source: Internet
Author: User

While loop syntax structure you can use the while loop while the Expression:while_suite statement while_suite the continuous loop execution until the value of the expression changes to 0 or false when the statement is repeated repeatedly.
# !/usr/bin/env python  == 1 while counter <=    :+ = Counter    + = 1print  "result is%d" % sum100
The break statement break statement can end the current loop and then jump to the next statement when writing the program, should try to avoid duplicate code, in which case you can use the While-break structure
#!/usr/bin/env python whileTrue:yn= Raw_input ("continue? (y/n)")    ifYninch 'Yy':         Break    Print " work on"/usr/bin/python2.6/root/pycharmprojects/untitled10/break1.pyContinue? (y/N) nwork onContinue? (y/N) yprocess finished with exit code 0
Continue statement When a continue statement is encountered, the program terminates the current loop and ignores the remaining statements. Then go back to the top of the loop. If the loop condition is still satisfied, the loop body statement continues execution, otherwise exits the loop with all even numbers and
# !/usr/bin/env python  =1 while counter <=:    + = 1    if counter% 2 = = 1:        
    continue    + = counterprint sum100
Else statement the while statement in Python also supports the ELSE clause else clause that executes a break statement only after the loop completes and skips the Else block
# !/usr/bin/env python  == 1 while I <=:    + = I    + = 1else:     Print SUM10

Python while loop

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.