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
4
votes
2 answers

PHP why is continue n slower than using break

Please consider the following code: $start = microtime(); for($i = 2; $i < 100; $i++) { for($y = 2; $y <= sqrt($i); $y++) { if($i%$y != 0) { continue; } else { continue 2; …
Gavin
  • 2,153
  • 2
  • 25
  • 35
4
votes
7 answers

Java Loops - Break? Continue?

I've read through a bunch of threads on using break and continue and I suspect the problem isn't necessarily my use of those, but the layout of my loops. In the following code, I am trying to iterate through the chars in a string that is input by…
Jesse Cleary
  • 123
  • 1
  • 7
4
votes
2 answers

multiple else if blocks or continue?

While solving a mindless task, a question came to my mind: /** * Find element by key in binary tree. */ public E find(K key) { BinaryTreeNode node = this.root; while (node != null) { if (node.getKey().compareTo(key) > 0) {…
Affe
  • 53
  • 1
  • 1
  • 7
4
votes
1 answer

difference between continue 2 and break in nested loops

I was refactoring some old code and stumbled on a continue 2 that can be easily replaced with a break. for($rows as $i){ for($columns as $j){ if( Helper::unicornExists($i, $j) ){ //continue 2; …
d.raev
  • 9,216
  • 8
  • 58
  • 79
4
votes
2 answers

java.util.ConcurrentModificationException But I am not removing

My below recursive function throws a ConcurrentModificationException on the 'continue' statement. I looked at a few posts on ConcurrentModificationException and all of the problems seem to be with removing an element from an element, but I am not…
Rohan
  • 1,312
  • 3
  • 17
  • 43
4
votes
5 answers

continue statement inside nested for loop

I don't understand what exactly does the continue statement inside this for loop do. How is code any different if I remove it? Which lines does it skip if it's placed at the end of for loop? int sum = 0, i, j, stop = 0; for( i = 1; i <= 5 && !stop;…
Firmus
  • 195
  • 1
  • 1
  • 8
4
votes
1 answer

Python: how to properly continue a while loop when an external exception occurs

I'm not a programmer, so very dumb Python question here. So, I have a script that bulk-check whois information for a list of domains. Here's a small example to show my problem: import pythonwhois as whois domainList = ['aaaa', 'bbbb', 'ccccc',…
4
votes
8 answers

is there a way to continue an exception in C#?

When an unexpected exception occurs in your program (in the debugger). Sometimes you just want to skip it since killing the program at that point is more harmful than continuing. Or you just want to continue since you were more interested in another…
Toad
  • 15,593
  • 16
  • 82
  • 128
4
votes
2 answers

using(s) inside a loop with a continue

Given the following sample code: var count = 0; while (count < 5) { using (var request = new HttpRequestMessage(HttpMethod.Get, requestUri)) using (var response = await StaticHttpClient.Client.SendAsync(request)) { if…
Pete Garafano
  • 4,863
  • 1
  • 23
  • 40
4
votes
7 answers

Javascript continue statement in while loop causes an infinite loop

I'm trying to create a while loop with a continue statement. However it seems to be causing an infinite loop and I can't figure out why. The code below to me seems like it should start with the var tasksToDo at 3 then decrement down to 0 skipping…
moonshineOutlaw
  • 723
  • 2
  • 9
  • 19
4
votes
1 answer

shell script : nested loop and continue

while [condition] do for [condition] do if [ "$x" > 3 ];then break fi done if [ "$x" > 3 ];then continue fi done In the above script I have to test "$x" > 3 twice. Actually the first time I test it, if it is true I want…
user1769686
  • 505
  • 2
  • 10
  • 16
3
votes
1 answer

Explanation of RAD Studio source code markers/visualizations

I was interested to know if the code markers shown at the end of some lines (see screenshot) are explained or documented anywhere (for RAD Studio 10.4). I notice that the first 3 (for Halt, Continue and Break) will disappear when these items are…
Paul
  • 41
  • 2
3
votes
1 answer

Is it possible to use continue keyword outside a loop in C++?

According to ISO C++: The continue statement shall occur only in an iteration-statement and causes control to pass to the loop-continuation portion of the smallest enclosing iteration-statement, that is, to the end of the loop. More precisely, in…
user15071942
3
votes
2 answers

Is the semicolon really a sequence point in C?

According to this answer, the following are the sequence points described in the standard: Between the evaluations of the function designator and actual arguments in a function call and the actual call; Between the evaluations of the first and…
Kushagr Jaiswal
  • 191
  • 1
  • 9
3
votes
5 answers

Continue in while inside foreach

In the following C# code snippet I have a 'while' loop inside a 'foreach' loop and I wish to jump to the next item in 'foreach' when a certain condition occurs. foreach (string objectName in this.ObjectNames) { // Line to jump to when…
Amr
  • 1,935
  • 2
  • 19
  • 29