user3060854
user3060854

Reputation: 933

python, how to skip lines in a range loop

In python language, I want to skip lines of a range loop (or xrange) without breaking the loop, as illustrated below:

for i in range(10):
    ... some code happening
    ... some code happening

    if (some statement == True):
        skip the next lines of the loop, but do not break the loop until finished

    ... some code happening
    ... some code happening

Upvotes: 3

Views: 2080

Answers (2)

kajarenc
kajarenc

Reputation: 103

Use continue

for i in range(10):
    if i == 5:
        continue
    print(i)

output:

 0
 1
 2
 3
 4
 6
 7
 8
 9

Upvotes: 4

Mureinik
Mureinik

Reputation: 311143

You could just nest that block in a condition:

for i in range(10):
    ... some code happening
    ... some code happening

    if not some_statement:

        ... some code happening
        ... some code happening

Upvotes: 1

Related Questions