Python3 loop _break and continue statement and else clause in loop

 

The break and continue statements and the else clause in the loop break statement can jump out of the loop body of for and while. If you terminate from a for or while loop, any corresponding loop else block will not execute.

The continue statement is used to tell Python to skip the remaining statements in the current loop block and continue with the next loop.

A loop statement can have an else clause; it is executed when the list is exhausted (with a for loop) or when the condition becomes false (with a while loop), but not when the loop is terminated by break. The following is an example of a loop that searches for prime numbers:

 

1 for n in range(2,10):
2     for x in range(2,n):
3         if n%x==0:
4             print(n,'equals',x,'*',n//x)
5             break
6     else:
7         print(n,'is a prime number')

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325134031&siteId=291194637