Break and continue

For loops can be exited earlier with the statement break. The rest of the cycle in the for loop can be skipped with the statement continue.

Input:

source_code/appendix_c_python/example12_break_continue.py
for i in range(0,10):
        if i % 2 == 1: #remainder from the division by 2
                continue
        print 'The number', i, 'is divisible by 2.'

for j in range(20,100):
        print j
        if j > 22:
                break;

Output:

$ python example12_break_continue.py 
The number 0 is divisible by 2.
The number 2 is divisible by 2.
The number 4 is divisible by 2.
The number 6 is divisible by 2.
The number 8 is divisible by 2.
20
21
22
23
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset