Questions tagged [loops]

Loops are a type of control flow structure in programming in which a series of statements may be executed repeatedly until some condition is met.

A loop is a fundamental programming idea that is commonly used in writing programs.

Types

A loop can be categorized in two different ways,

1. Entry Controlled Loops

A loop which checks for the condition before the iteration is known as Entry Controlled loops - for example

  • while loop - iterates while a certain condition is true
  • until loop - iterates while a certain condition is false
  • for loop - iterates through numbers in a certain range. Note: not the same as C++ for loop
  • foreach loop - iterates through the elements of a collections.

2. Exit Controlled Loops

A loop which checks the condition after the iteration is knows as Exit Controlled loop - for example

  • do-while loop - iterates while a certain condition is true (the first iteration will run regardless of the condition)
  • do-until loop - iterates while a certain condition is false (the first iteration will run regardless of the condition)

Most languages provide only a subset of loop types described above. For example: in Python there are only foreach (keyword for) and while loops.

Break and continue

In some languages, there are two keywords that simplify the task of implementing a more advanced control flow: break and continue. The former allows you to jump to the operator immediately after the loop, the latter allows you to jump to the end of the current iteration.

Example: implementation of do-until loop in Python using the break keyword:

while True:
    // loop body
    if condition:
        break

Tag usage

The tag can be used for programming related problems in implementing loops feature of any programming language. Please avoid theoretical questions related to tag on stackoverflow.

See also:

Read more

95843 questions
9
votes
4 answers

How to remove elements from a queue in Java with a loop

I have a data structure like this: BlockingQueue mailbox = new LinkedBlockingQueue(); I'm trying to do this: for(Mail mail: mailbox) { if(badNews(mail)) { mailbox.remove(mail); } } Obviously the contents of the loop interfere…
Josh
  • 635
  • 2
  • 7
  • 12
9
votes
3 answers

How to traverse Linked-Lists Python

I am trying to figure out how I can traverse linked list in Python using Recursion. I know how to traverse linked-lists using common loops such as: item_cur = my_linked_list.first while item_cur is not None: print(item_cur.item) …
Andre
  • 1,601
  • 6
  • 19
  • 19
9
votes
2 answers

Loop through a dictionary in swift

I'm trying to replicate a for in loop I did in Objective C but am getting an "'AnyObject' does not have a member named 'GeneratorType' error: for (NSString *file in [dict objectForKey:@"Files"]){ NSString *content = [[source…
krisacorn
  • 831
  • 2
  • 13
  • 23
9
votes
2 answers

Does a loop take longer to execute each time?

I'm working on some J2EE project which involves storing postal codes, cities and countries together. We have developed a Java class which handles the integration of every country file (containing each postal code and each city). The problem is that…
Adrien Dos Reis
  • 472
  • 2
  • 14
9
votes
6 answers

jquery click function inside a php loop

I have a foreach loop in php like below.
Joel
Joel James
  • 1,315
  • 1
  • 20
  • 37
9
votes
9 answers

Loop on enumeration values

How awful is it - or is it perfectly acceptable - to index a loop on an enumeration? I have an enumeration defined. The values of the literals are default values. The assigned values do not have any significance, will not have any significance, and…
Rachel
  • 2,181
  • 2
  • 18
  • 20
9
votes
6 answers

Java do while, while

what behaviour can I expect when I run this code: do while(testA) { // do stuff } while(testB); Will it behave like: do { while(testA) { // do stuff } } while(testB); Or: if(testA) { do { // do stuff }…
Pindatjuh
  • 10,550
  • 1
  • 41
  • 68
9
votes
7 answers

Is a big loop within a small loop always faster than a small loop within a big one?

I just read this post, and wonder if we can draw the conclusion that a big loop within a small loop must always run faster than a small loop within a big one, no matter what the code does inside the nested loop? Take an example. int m, n; m =…
Wallace
  • 561
  • 2
  • 21
  • 54
9
votes
3 answers

Running a loop while debugging VBA

The Problem I am trying to debug some code, and somewhere in the middle I stopped at a breakpoint. Now I want to change some variables and run a certain loop several times. How far did I get? I know how to change the variables, but somehow I get…
Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122
9
votes
4 answers

"Surround with"-template in Eclipse: foreach

I am new to Eclipse which I use primarily for Java. I have previously used IntelliJ Idea in which it is possible to select a variable which extends Iteratable (Collection, List etc) and have it produce a correct foreach loop. I know Eclipse does…
Casper
  • 368
  • 4
  • 10
9
votes
3 answers

Applying revgeocode to a list of longitude-latitude coordinates

I'm trying to get the Zip codes of a (long) list of Longitude Latitude coordinates by using the revgeodcode function in the ggmap library. My question & data are the same as here: Using revgeocode function in a FOR loop. Help required but the…
Rutger
  • 129
  • 1
  • 6
9
votes
4 answers

Python - While false loop

fn='a' x=1 while fn: print(x) x+=1 if x==100: fn='' Output: 1 ... 99 fn='' x=1 while fn: print(x) x+=1 if x==100: fn='a' Output: while loop does not run. What is the reason for the while loop not…
Phoenix
  • 4,386
  • 10
  • 40
  • 55
9
votes
2 answers

jQuery Looping Animation pauses each time. How to keep from pausing?

I am trying to cause a block to "pulsate" between 100% opacity and some partially transparent opacity. I want to do this with the functionality that is built into the jQuery core, if possible. I would rather not add a plugin to get this effect. Here…
Tim Mackey
  • 323
  • 3
  • 16
9
votes
4 answers

Updating a pyplot 3d scatter plot in a loop, grid lines overlap points

I am updating a 3d scatter plot with every iteration of a loop. When the plot is redrawn, the gridlines "go through" or "cover" the points, which makes my data more difficult to visualize. If I build a single 3d plot (no loop updating) this does not…
NLi10Me
  • 3,182
  • 2
  • 13
  • 15
9
votes
2 answers

extended initializer lists only available with

I'm very new to C++ and I'm having trouble reading my errors I was able to eliminate most of them but I'm down to a few and I'm request help on them please. Here is the program #include #include using namespace std; int main(){ …
Christian Gardner
  • 237
  • 2
  • 3
  • 6
1 2 3
99
100