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
43
votes
7 answers

Why is `continue` not allowed in a `finally` clause in Python?

The following code raises a syntax error: >>> for i in range(10): ... print i ... try: ... pass ... finally: ... continue ... print i ... File "", line 6 SyntaxError: 'continue' not supported inside 'finally'…
ElenaT
  • 2,600
  • 4
  • 24
  • 41
43
votes
7 answers

Applescript equivalent of "continue"?

I have a simple 'repeat with' in an AppleScript, and would like to move on to the next item in the "repeat" conditionally. Basically I'm looking for something similar to "continue" (or break?) in other languages. I'm not well versed in AppleScript…
danieljimenez
  • 1,390
  • 4
  • 17
  • 26
41
votes
9 answers

'CONTINUE' keyword in Oracle 10g PL/SQL

I'm migrating a TSQL stored procedure to PL/SQL and have encountered a problem - the lack of a CONTINUE keyword in Oracle 10g. I've read that Oracle 11g has this as a new feature, but upgrading is not an option unfortunately. Is there any…
Chris McAtackney
  • 5,192
  • 8
  • 45
  • 69
38
votes
10 answers

When to use the 'continue' keyword in C#

Recently, I was going through an open-source project and although I have been developing for several years in .NET, I hadn't stumbled across the continue keyword before. Question: What are some best practices or areas that would benefit from using…
Rion Williams
  • 74,820
  • 37
  • 200
  • 327
36
votes
8 answers

Break for loop from inside of switch case in Javascript

What command I must use, to get out of the for loop, also from //code inside jump direct to //code after //code before for(var a in b) { switch(something) { case something: { //code inside …
BASILIO
  • 847
  • 2
  • 12
  • 26
35
votes
7 answers

if pass and if continue in python

I saw someone posted the following answer to tell the difference between if x: pass and if x: continue. >>> a = [0, 1, 2] >>> for element in a: ... if not element: ... pass ... print(element) ... 0 1 2 >>> for element in a: ... …
Shelly
  • 569
  • 2
  • 5
  • 7
33
votes
3 answers

Python: Using continue in a try-finally statement in a loop

Will the following code: while True: try: print("waiting for 10 seconds...") continue print("never show this") finally: time.sleep(10) Always print the message "waiting for 10 seconds...", sleep for 10…
Andres Riofrio
  • 9,851
  • 7
  • 40
  • 60
30
votes
4 answers

How to use something like a continue statement in nested for loops?

I have a class of objects and need to compare one property of each object to the same property of all other objects. If they match, the code needs to do something. This results in two 'for loops' looping through the objects to get that property, and…
faranzki
  • 403
  • 1
  • 4
  • 6
28
votes
9 answers

Python: How to tell the for loop to continue from a function?

Sometimes I need the following pattern within a for loop. At times more than once in the same loop: try: # attempt to do something that may diversely fail except Exception as e: logging.error(e) continue Now I don't see a nice way to…
fmalina
  • 6,120
  • 4
  • 37
  • 47
28
votes
3 answers

Is it possible to use 'yield' to generate 'Iterator' instead of a list in Scala?

Is it possible to use yield as an iterator without evaluation of every value? It is a common task when it is easy to implement complex list generation, and then you need to convert it into Iterator, because you don't need some results...
yura
  • 14,489
  • 21
  • 77
  • 126
28
votes
9 answers

How does continue work?

I am trying to understand how "continue" works. I understood the concept of the keyword but when I run different programs, it works differently :-/ Let me show you few examples: If I run this program : int j = 0; int i = 0; LABEL1: for (; i < 3;…
user3884432
  • 291
  • 3
  • 6
26
votes
3 answers

Check if file exists and continue else exit in Bash

I have a script that is one script in a chain of others that sends an email. At the start of the script I want to check if a file exists and continue only if it exists, otherwise just quit. Here is the start of my script: if [ ! -f /scripts/alert…
user1190083
  • 405
  • 1
  • 4
  • 5
25
votes
9 answers

ForEach() : Why can't use break/continue inside

Since ForEach() method loop through all list members, Why can't I use a break/continue clause while I can use them inside a normal foreach loop lstTemp.ForEach(i=> { if (i == 3) break; //do sth } ); Error: "No enclosing loop out of…
Rami Alshareef
  • 7,015
  • 12
  • 47
  • 75
24
votes
2 answers

syntaxError: 'continue' not properly in loop

I have been struggling with this error for a while now and there seems to be different opinions regarding why the interpreter complains about the 'continue'. So I would like to provide the erroneous code below. import tweepy import time def…
anonuser0428
  • 11,789
  • 22
  • 63
  • 86
24
votes
4 answers

break and continue in function

def funcA(i): if i%3==0: print "Oh! No!", print i break for i in range(100): funcA(i) print "Pass", print i I know script above won't work. So, how can I write if I need put a function with break or continue into a…
ThunderEX
  • 703
  • 1
  • 7
  • 13
1
2
3
48 49