Example Use of "Continue" Statement in Python

Example use of continue statement in Python?

Here's a simple example:

for letter in 'Django':    
if letter == 'D':
continue
print("Current Letter: " + letter)

Output will be:

Current Letter: j
Current Letter: a
Current Letter: n
Current Letter: g
Current Letter: o

It skips the rest of the current iteration (here: print) and continues to the next iteration of the loop.

Please explain the continue statement

The continue statement causes Python to skip the rest of the current iteration of the loop, and jump to the beginning of the next iteration.

See this documentation page for Python 3. The original example on that page is:

>>> for num in range(2, 10):
... if num % 2 == 0:
... print("Found an even number", num)
... continue
... print("Found a number", num)

Found an even number 2
Found a number 3
Found an even number 4
Found a number 5
Found an even number 6
Found a number 7
Found an even number 8
Found a number 9

When continue is encountered, the print at the end of the loop is skipped, and execution goes to the for again to get the next iteration. Note how for even numbers, "Found an even number" is printed, but "Found a number" is not printed. This is because the continue skipped the rest of the loop body.


Your modification to the sample - inserting the else - makes the continue obsolete, because the print("Found a number", num) wouldn't be executed anyway (it sits in an else) branch.

This way you've discovered that continue (and also break) are often an alternative control flow mechanism to if...else. Which to use depends on the situation and style preferences.

Is there a difference between continue and pass in a for loop in Python?

Yes, they do completely different things. pass simply does nothing, while continue goes on with the next loop iteration. In your example, the difference would become apparent if you added another statement after the if: After executing pass, this further statement would be executed. After continue, it wouldn't.

>>> a = [0, 1, 2]
>>> for element in a:
... if not element:
... pass
... print(element)
...
0
1
2
>>> for element in a:
... if not element:
... continue
... print(element)
...
1
2

IF statements not calling my data after continue statement

added an else pass statement and got rid of the continue statement. Thank you all for the help!

Is the continue statement necessary in a while loop?

Here's a really simple example where continue actually does something measureable:

animals = ['dog', 'cat', 'pig', 'horse', 'cow']
while animals:
a = animals.pop()
if a == 'dog':
continue
elif a == 'horse':
break
print(a)

You'll notice that if you run this, you won't see dog printed. That's because when python sees continue, it skips the rest of the while suite and starts over from the top.

You won't see 'horse' or 'cow' either because when 'horse' is seen, we encounter the break which exits the while suite entirely.

With all that said, I'll just say that over 90%1 of loops won't need a continue statement.

1This is complete guess, I don't have any real data to support this claim :)

How else part work in continue statement?

Your else part will be executed in both cases.
else part executed when loop terminate when condition didn't found.Which is what is happening in your code. But it will also work same without continue statement.

now what about break statement's else part, Break statement's else part will be executed only if:

  • If the loop completes normally without any break.
  • If the loop doesn't encounter a break.

Sample Image

Python 3 Use case for continue instead of not (!=)

continue for a loop is just like return to a function: a convenience instruction to skip to the next iteration right now.

On a complex case, continue can skip to the next iteration very simply:

for i in range(0, 10):
if i != 3:
print("something")
if my_function(i) != 34:
continue
print(i)

To do that without continue, you need a flag or else conditions. Careful as if there are a lot of continue statements in your loops it can become difficult to debug (just like when you put too many return statements in a function)

Could someone explain the continue key in the if statements?

Understand that continue does not break out of if and elif statements. Continue only breaks out of looping statements.

If continue is used within an if block which is present inside a loop, it skips the current iteration of the loop. So when continue is encountered, control moves to the beginning of the loop skipping any statements after continue. In this program, if user enters 'multiple letters', an 'already guessed letter' or a 'non-alphabet', continue comes into play and the program just skips that iteration and goes on to accept another letter.

while cond1:
if cond2:
continue
#code

So here, if cond2 is satisfied, continue is encountered and #code is not executed. It just goes to the while cond1: again and continues the loop

How to use continue in single line if esle within a for loop

There is no such thing as a single line if condition in Python. What you have in your first example is a ternary expression.

You are trying to assign continue to a variable named value, which does not make any sense, hence the syntax error.



Related Topics



Leave a reply



Submit