Python Foundation 2.4 While Loop

Source: Internet
Author: User



# @Time

# @File : While loop. py

Example 1:
1 n = 02 while True:3     print'Hello World'4     if n = = Ten:5break         6      N +=1
Print as follows:
1 Hello World 2 Hello World 3 Hello World 4 Hello World 5 Hello World 6 Hello World 7 Hello World 8 Hello World 9 Hello World Ten Hello World  Hello World

Example 2:

1 n = 02 while True:3     if n==10:4 break         5     print N,'hello'6      N +=1

Print as follows:

1 0 Hello 2 1 Hello  3 2 Hello  4 3 Hello  5 4 Hello  6 5 Hello  7 6 Hello  8 7 Hello  9 8 Hello
    
     10 9 Hello
    


Example 3:

#条件为假的时候, also exits the loop. Exit the loop when you enter a null character
1Rinput ="'2  whileRinput! ='Q':3Rinput = Raw_input ('Please input something,q is quite:')4     Print 'Hello'5     ifRinput = ="':6          Break


Example 4:

#应用逻辑非判断, exit the while loop. While condition is false, exits while loop
1Rinput ="'2  whileRinput! ='Q':3Rinput = Raw_input ('Please input something,q is quite:')4     Print 'Hello'5     if  notRinput:6          Break

Example 5:

# While...else ...: The statement that follows else is printed when the normal end while loop. With For...else ... The same as the loop body
1Rinput ="'2  whileRinput! ='Q':3Rinput = Raw_input ('Please input something,q is quite:')4     Print 'Hello'5     if  notRinput:6          Break7 Else:8     Print 'Hello World'

Example 6:

#while loop nested If loop body, support contine
1Rinput ="'2  whileRinput! ='Q':3Rinput = Raw_input ('Please input something,q is quite:')4     Print 'Hello'5     if  notRinput:6          Break7     elifRinput = ='quite':8         Continue9         Print 'Continue'Ten Else: One     Print 'Hello World'

Python Foundation 2.4 While Loop

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.