+ 1
I didn't understood continue statement
11 Answers
+ 4
It's not much difficult as you think.
Let's see a simple example.
for i in range(3):
print(i)
continue
print(i)
If we remove continue statement in above code, we might expect the output is
001122
Here, we added continue statement. So, output is
012
Why?
It's because, the continue statement restart the current position of loop.
It means, It will not execute the further code are go to next count/execution.
+ 2
continue is used to skip an iteration of a loop based on certain condition
# Printing without continue statement
for i in range(0, 5):
print(i)
# Output: 0 1 2 3 4
# Printing with continue statement
for i in range(0, 5):
if i == 2:
continue
print(i)
# Output: 0 1 3 4
# It skipped 2 because we put condition that if variable has value 2 then skip(continue) an iteration
Now let's use this for printing even numbers
for i in range(0, 11):
if i % 2 != 0:
continue
print(i)
# Output: 0 2 4 6 8 10
continue statement is used above any single statement or group of statements that you want to ignore for some condition inside a loop
+ 1
The continue statement in Python returns the control to the beginning of the current loop. When encountered, the loop starts next iteration without executing the remaining statements in the current iteration. The continue statement can be used in both while and for loops.
gargoyle = 30
steel_axe = 1
fighting = True
while fighting:
gargoyle=gargoyle - 1
print ("You attacked the gargoyle! HP left:")
print (gargoyle)
if gargoyle == 15:
print ("He's halfway dead!")
continue
elif gargoyle == 10:
print ("Take him down!")
continue
elif gargoyle == 5:
print ("Now's your chance")
continue
if gargoyle <= 0:
print ("Dead!")
break
print("Congrats!")
In the code above, whenever continue is reached, the code below is ignored and the next iteration starts/control reaches to while.
I hope this clears your query.
Code by:
https://www.sololearn.com/Profile/3203399/?ref=app
+ 1
Tq for ur help
+ 1
Suppose u have a loop
In that loop u don't want an iteration to work
So you can write this
for( int x=1 ; x<10;x++)
{
If(x==7)
Continue;
System.out.println(x);
}
That continue will not let the rest of the body to run when x==7
#Output
1
2
3
4
5
6
8
9
0
When continue is used, iteration will straight go to the next iteration without passing the GO (Joke about monopole board game, jail messages always said "without passing the GO".
0
Can u please be more specific
0
I am a beginner
What is console
0
Translation:
9ck k blplu lol people I am uppon 9km long, people I am 9, look mmmhmm o (speaking while eating)
- 1
9cf Ä· blplu lol ppl m ivon kml9 ppl I'll j on I'm nin lo0k mmmhmm o