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

Why can't I use 'continue' inside a switch statement in Java?

Why is it that the following code: class swi { public static void main(String[] args) { int a=98; switch(a) { default:{ System.out.println("default");continue;} case 'b':{…
abson
  • 9,148
  • 17
  • 50
  • 69
10
votes
3 answers

Alternative for 'continue' keyword

I was browsing through questions regarding continue keyword to get a better understanding of it and I stumbled upon this line in this answer These can be maintenance timebombs because there is no immediate link between the "continue"/"break" and…
Anjan Baradwaj
  • 1,219
  • 5
  • 27
  • 54
9
votes
4 answers

PHP continue inside function

This is likely very trivial but I haven't been able to figure it out. This works: function MyFunction(){ //Do stuff } foreach($x as $y){ MyFunction(); if($foo === 'bar'){continue;} //Do stuff echo $output . '
'; } But this…
Clarissa B
  • 239
  • 2
  • 5
  • 14
9
votes
5 answers

Why it is a bad practice to use break/continue labels in OOP (e.g. Java, C#)?

I was told that using break and continue labels in an OOP language is not OOP programming style. Can you explain in detail why and what is the problem? The trick was with this label word. I meant labeled break/continue. class BreakWithLabelDemo { …
Andrii Muzychuk
  • 1,151
  • 3
  • 20
  • 28
8
votes
4 answers

'continue' the 'for' loop to the previous element

I have been trying to find a way to continue my for loop to the previous element. It's hard to explain. Just two be clear, here is an example: foo = ["a", "b", "c", "d"] for bar in foo: if bar == "c": foo[foo.index(bar)] = "abc" …
Nouman
  • 6,947
  • 7
  • 32
  • 60
8
votes
5 answers

Difference between if: else: and if: continue

Let's say I have an iterable: balls. I want to do something to all the balls in that loop that are not blue. As far as I see it I have two options: Using if: else: for ball in balls: if ball.blue: # can do something with blue ball …
leafystar
  • 129
  • 1
  • 1
  • 7
8
votes
3 answers

Why is continue inside a loop a bad idea?

Douglas Crockfod says that it is usually better to refactor the continue inside the loop. Why is continue considered bad within a loop?
unj2
  • 52,135
  • 87
  • 247
  • 375
8
votes
2 answers

Bash script continue keyword not breaking loop iteration

I am following a series of tutorials to learn Bash shell script. One of the exercises is to loop through files in the current directory and search for a pattern in those files. If the pattern is found then the script should sum the file size of…
Drok
  • 113
  • 1
  • 7
8
votes
3 answers

Please explain the usage of Labeled Statements

Is breaking and continuing the only uses of labeled statements in Java? When have you used Labeled Statements in your programs? Sorry the code snippet has been deleted. I am splitting the question
unj2
  • 52,135
  • 87
  • 247
  • 375
8
votes
1 answer

Jinja2 template from Flask is failing to render CONTINUE statement

I am trying a simple continue inside a for-loop in Flask with jinja2 {% for num in range(0,10) %} {% if num%2 == 0 %} {% print num %} {% else %} {% continue %} {% endif %} and i get this error TemplateSyntaxError: Encountered unknown…
Rakib
  • 12,376
  • 16
  • 77
  • 113
7
votes
6 answers

C# - foreach loop within while loop - break out of foreach and continue on the while loop right away?

while (foo() == true) { foreach (var x in xs) { if (bar(x) == true) { //"break;" out of this foreach //AND "continue;" on the while loop. } } //If I didn't continue, do other stuff. } I'm a…
myermian
  • 31,823
  • 24
  • 123
  • 215
7
votes
4 answers

continue statement in a do while loop

#include #include enum {false, true}; int main() { int i = 1; do { printf("%d\n", i); i++; if (i < 15) continue; } while (false); getchar(); return 0; } What…
user13027222
7
votes
5 answers

Converting a for-loop with continue statement to while-loop

I am working on an exercise where a small piece of code based on a for-loop is converted to preform the same operation with a while loop. The conversion is wrong by purpose, and looks like this: int sum = 0; for (int i = 0; i < 4; i++) { …
Esben86
  • 483
  • 8
  • 21
7
votes
1 answer

Using continue in python ternary?

How can I use continue in python ternary? Is that even possible? E.g. >>> for i in range(10): ... x = i if i == 5 else continue give a SyntaxError: invalid syntax If continue in ternary is possible, is there any other way of doing this: >>>…
alvas
  • 115,346
  • 109
  • 446
  • 738
7
votes
3 answers

In MATLAB, how can I skip a pre-determined number of for loop iterations if certain criteria is met?

In Matlab, I am performing calculations during a for loop but am trying to find a way to skip iterations during the for loop if certain criteria are met. I have written out a quick example to illustrate my question. In the code below, the for loop…