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.
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.
I like to use continue in loops where there are a lot of contitions to be fulfilled before you get "down to business". So instead of code like this:
for x, y in zip(a, b):
if x > y:
z = calculate_z(x, y)
if y - z < x:
y = min(y, z)
if x ** 2 - y ** 2 > 0:
lots()
of()
code()
here()
I get code like this:
for x, y in zip(a, b):
if x <= y:
continue
z = calculate_z(x, y)
if y - z >= x:
continue
y = min(y, z)
if x ** 2 - y ** 2 <= 0:
continue
lots()
of()
code()
here()
By doing it this way I avoid very deeply nested code. Also, it is easy to optimize the loop by eliminating the most frequently occurring cases first, so that I only have to deal with the infrequent but important cases (e.g. divisor is 0) when there is no other showstopper.
Videos
How does the continue statement work in Python?
Why do we need the continue statement in Python?
What is the use of continue in Python?
So, I tried searching the group and I found people asking *what* the continue keyword does, which is not my question. I think I understand it. Basically, it just says "hey, if x condition is met do not do what you did to every other element in the loop. Potentially do this instead or Just go to the next item."
The question I have is why should I use it instead of just an if-esle statement,, or if you prefer continue why should I use an if-else and not default to continue.
To put it into context, what is the meaningful difference between the following code blocks:
for i in range(10):
if i == 7:
print('7? I hate prime numbers bigger than 5!')
continue
print(f'Woo! I love the number {i}')and
for i in range(10):
if i == 7:
print('7? I hate prime numbers bigger than 5!')
else:
print(f'Woo! I love the number {i}')Both got me the same result. Is it just a "Python has many ways to do the same thing" deal or am I missing a crucial difference?
"continue" applies to loops.
It will simply stop the current iteration and jump into the next. So basically keep typing what you are typing until you type what the program expects of you.
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
I am new to learning Python or rather I say new to programming. So, anyway
Can anyone help me understanding the "continue" function in Python statement(for loop while loop etc) or help me with a link with broad explanation?
I get "break" , "pass" but can't quite understand the use of "continue".
The continue command restarts the innermost loop at the condition.
That means after x reaches 33, x += 1 will never execute because you will be hitting the continue and going back to the while line without running the rest of the code block.
x will forever be 33 so you will have a infinite loop.
You are skipping the increment that happens at the end of the while loop when you call continue. The following will automatically increment if you want to keep the continue statement:
for x in range(50):
if x == 33:
print("I hit 33")
continue
else:
print(x)
Otherwise, delete the continue.