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
747
votes
8 answers

Equivalent of "continue" in Ruby

In C and many other languages, there is a continue keyword that, when used inside of a loop, jumps to the next iteration of the loop. Is there any equivalent of this continue keyword in Ruby?
Mark Szymanski
  • 56,590
  • 24
  • 70
  • 87
373
votes
13 answers

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

Is there any significant difference between the two Python keywords continue and pass like in the examples for element in some_list: if not element: pass and for element in some_list: if not element: continue I should be…
Aufwind
  • 25,310
  • 38
  • 109
  • 154
314
votes
12 answers

What is the "continue" keyword and how does it work in Java?

I saw this keyword for the first time and I was wondering if someone could explain to me what it does. What is the continue keyword? How does it work? When is it used?
faceless1_14
  • 7,382
  • 9
  • 31
  • 33
297
votes
21 answers

Difference between break and continue statement

Can anyone tell me the difference between break and continue statements?
DonX
  • 16,093
  • 21
  • 75
  • 120
187
votes
10 answers

Example use of "continue" statement in Python?

The definition of the continue statement is: The continue statement continues with the next iteration of the loop. I can't find any good examples of code. Could someone suggest some simple cases where continue is necessary?
JohnG
  • 2,109
  • 3
  • 16
  • 12
128
votes
6 answers

Using continue in a switch statement

I want to jump from the middle of a switch statement, to the loop statement in the following code: while (something = get_something()) { switch (something) { case A: case B: break; default: // get another…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
77
votes
11 answers

Continue in nested while loops

In this code sample, is there any way to continue on the outer loop from the catch block? while { // outer loop while { // inner loop try { throw; } catch { // how do I…
SkunkSpinner
  • 11,429
  • 7
  • 40
  • 53
69
votes
2 answers

How do I do a "break" or "continue" when in a functional loop within Kotlin?

In Kotlin, I cannot do a break or continue within a function loop and my lambda -- like I can from a normal for loop. For example, this does not work: (1..5).forEach { continue@forEach // not allowed, nor break@forEach } There are old…
Jayson Minard
  • 84,842
  • 38
  • 184
  • 227
66
votes
6 answers

Why are "continue" statements bad in JavaScript?

In the book Javascript: The Good Parts by Douglas Crockford, this is all the author has to say about the continue Statement: The continue statement jumps to the top of the loop. I have never seen a piece of code that was not improved by refactoring…
twiz
  • 9,041
  • 8
  • 52
  • 84
62
votes
4 answers

php foreach continue

I am trying to skip to the next iteration of the loop if certain conditions are not met. The problem is that the loop is continuing regardless. Where have I gone wrong? Updated Code sample in response to first comment. foreach ($this->routes as…
ComputerUser
  • 4,828
  • 15
  • 58
  • 71
56
votes
4 answers

What is meant by a number after "break" or "continue" in PHP?

Could someone please explain, with examples, what is meant by loop break 2 or continue 2 in PHP? What does it mean when break or continue is followed by a number?
tree em
  • 20,379
  • 30
  • 92
  • 130
55
votes
7 answers

Skip multiple iterations in loop

I have a list in a loop and I want to skip 3 elements after look has been reached. In this answer a couple of suggestions were made but I fail to make good use of them: song = ['always', 'look', 'on', 'the', 'bright', 'side', 'of', 'life'] for sing…
Mehdi Nellen
  • 8,486
  • 4
  • 33
  • 48
51
votes
9 answers

"Continue" (to next iteration) on VBScript

A colleague and I were trying to figure out a way of doing the equivalent of a "continue" statement within a VBScript "For/Next" loop. Everywhere we looked we found people had no way to do this in VBScript without having nasty nestings, which is not…
EKI
  • 838
  • 1
  • 8
  • 15
49
votes
3 answers

How to use "continue" in groovy's each loop

I am new to groovy (worked on java), trying to write some test cases using Spock framework. I need the following Java snippet converted into groovy snippet using "each loop" Java Snippet: List myList = Arrays.asList("Hello", "World!", "How",…
Karthikeyan
  • 569
  • 1
  • 5
  • 10
45
votes
11 answers

Nested jQuery.each() - continue/break

Consider the following code: var sentences = [ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', 'Vivamus aliquet nisl quis velit ornare tempor.', 'Cras sit amet neque ante, eu ultrices est.', 'Integer…
Ryan Kinal
  • 17,414
  • 6
  • 46
  • 63
1
2 3
48 49