The Else statement usage of the Python loop statement, which executes the else statement when the loop condition becomes false and the cut is not terminated by Breakbreak.

Source: Internet
Author: User

A looping statement can have an else clause , when the (for) loop iterates through the entire listor (while) the loop condition becomes false, rather than by the break statement. The Else statement is executed. the following code, which loops through the search for prime numbers, illustrates this:

>>>
>>>ForNInchRange(2,10):...ForXInchRange(2,N):...IfN%X==0:...PrintNx ' * ' n/x ... breakelse: ... # loop fell through without finding a factor ... print n ' is a prime number ' ...< Span class= "Go" >2 is a prime Number3 is a prime Number4 equals 2 * 25 I s a prime Number6 equals 2 * 37 is a prime Number8 equals 2 * 49 equals 3 * 3               

(yes, this is the correct code.) Look carefully: theelse clause belongs to the for loop and is not part of the if statement. )

The Else statement usage of the Python loop statement, which executes the else statement when the loop condition becomes false and the cut is not terminated by Breakbreak.

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.