The difference between break and continue in python

1. Break statement

The break statement is usually used in for loops and while loops. Its function is to terminate the current loop. It is generally used in conjunction with the if statement to indicate that it will jump out of the current loop under a certain condition.

Example:

#!/usr/bin/env python
# -*- coding:utf-8 -*-

if __name__ == '__main__':
    for i in range(0, 5):
        if i == 2:
            break
        print(i)

output:

0
1

2. continue statement

The continue statement is used to jump out of this loop, that is, to skip the remaining code in the loop body of this loop and execute the next loop.

#!/usr/bin/env python
# -*- coding:utf-8 -*-

if __name__ == '__main__':
    for i in range(0, 5):
        if i == 2:
            continue
        print(i)

output:

0
1
3
4

Guess you like

Origin blog.csdn.net/kevinjin2011/article/details/129321918