Questions tagged [continue]

A language construct typically used to bypass the rest of a loop and return to the beginning for the next iteration.

Using continue will go back to the first line of the loop, in this example when i is 2 or 3 the program will immediately go back to the first line: for i in range(1, 6) before the current iteration has finished

Example (Python):

for i in range(1, 6):
    print(i)
    if i == 2 or i == 3:
        continue
    print("do stuff")
print("after the loop")

Output:

1
do stuff
2
3
4
do stuff
5
do stuff
after the loop

Also see .

724 questions
24
votes
6 answers

How to continue executing code after calling ShowDialog()

the Form.ShowDialog() method causes the code to be halted until the newly called form is closed. I need the code to continue running after the ShowDialog() method is called. I googled and read about using backgroundworker? But that is the first time…
Seth
  • 1,769
  • 4
  • 26
  • 39
22
votes
17 answers

Continue Considered Harmful?

Should developers avoid using continue in C# or its equivalent in other languages to force the next iteration of a loop? Would arguments for or against overlap with arguments about Goto?
Brian
  • 5,826
  • 11
  • 60
  • 82
21
votes
6 answers

Java Continue Label is Deprecated?

I have 2 fors, after the nested for I have some code which I don't want to execute if a condition is true inside the nested for. If I use break that code would execute, so (as I learned in SCJP) I used continue label; for the outer for. Is this a…
Cosmin Cosmin
  • 1,526
  • 1
  • 16
  • 34
20
votes
10 answers

C#: Nested conditionals vs continue statement

In using ReSharper recently, it is suggesting I reduce nesting in certain places by inverting if conditions and using the continue statements. nested conditionals: foreach(....) { if(SomeCondition) { //do some things …
KP.
  • 13,218
  • 3
  • 40
  • 60
20
votes
1 answer

Using `continue` keywoard in a switch nest inside a foreach loop

I have the code below (which actually is much longer than you see!) foreach (SensorPair sensor in _sensorPairs) { sensorByte = (byte) sensor.Sensor; if (!packet.Contains(sensorByte)) continue; index =…
Dumbo
  • 13,555
  • 54
  • 184
  • 288
19
votes
7 answers

Using continue in a do-while loop

MDN states: When you use continue without a label, it terminates the current iteration of the innermost enclosing while, do-while or for statement and continues execution of the loop with the next iteration. I'm not sure why the following piece of…
pimvdb
  • 151,816
  • 78
  • 307
  • 352
15
votes
6 answers

c++ continue versus break

Which statement will be executed after "continue" or "break" ? for(int i = 0; i < count; ++i) { // statement1 …
eugene
  • 39,839
  • 68
  • 255
  • 489
15
votes
8 answers

Python - Way to restart a for loop, similar to "continue" for while loops?

Basically, I need a way to return control to the beginning of a for loop and actually restart the entire iteration process after taking an action if a certain condition is met. What I'm trying to do is this: for index, item in enumerate(list2): …
Georgina
  • 381
  • 1
  • 6
  • 13
15
votes
2 answers

php - Nested Loop, Break Inner Loops and Continue The Main Loop

I have the following loop and I want to continue the while loop when the check inside the inner loop has meet the condition. I found a solution here (which is I applied in the following example), but it is for c#. $continue = false; …
Ari
  • 4,643
  • 5
  • 36
  • 52
14
votes
1 answer

continue n times in kotlin loop

Is it possible to continue n times in a kotlin loop? for(i in 0..n){ doSomething() if(condition){ //manipulate n } } Since i for some reason is a val I cannot seem to reinitialize it in the loop.
Jonas Grønbek
  • 1,709
  • 2
  • 22
  • 49
13
votes
11 answers

How to return a specific point after an error in 'while' loop

I'm trying to write a program that include a while loop, in this loop I have an error message if something goes wrong. It's kinda like this; while True: questionx = input("....") if x =="SomethingWrongabout questionX": print…
GLHF
  • 3,835
  • 10
  • 38
  • 83
11
votes
4 answers

Why is this labeled javaScript continue not working?

I am using this code to weather some circles are overlapping: iCantThinkOfAGoodLabelName: x = genX(radius); y = genY(radius); for(i in circles) { var thisCircle = circles[i]; if(Math.abs(x-thisCircle["x"])+Math.abs(y-thisCircle["y"])>radius*2) {…
JJJollyjim
  • 5,837
  • 19
  • 56
  • 78
11
votes
8 answers

Why is 'continue' statement ignoring the loop counter increment in 'while' loop, but not in 'for' loop?

Why does it tend to get into an infinite loop if I use continue in a while loop, but works fine in a for loop? The loop-counter increment i++ gets ignored in while loop if I use it after continue, but it works if it is in for loop. If continue…
Thokchom
  • 1,602
  • 3
  • 17
  • 32
11
votes
4 answers

Java continue at the end of if

I have some example code from a book and the author is always using an continue at the end of an if. Example: int a = 5; if(a == 5) { // some code continue; } Now for me this doesn't make any sense. Might there be some kind of quality management…
John Frost
  • 673
  • 1
  • 10
  • 24
10
votes
2 answers

``continue`` breaks label placement

This works just fine: #include int main(){ volatile int abort_counter = 0; volatile int i = 0; while (i < 100000000) { __asm__ ("xbegin ABORT"); i++; __asm__ ("xend"); __asm__ ("ABORT:"); …
User1291
  • 7,664
  • 8
  • 51
  • 108
1 2
3
48 49