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
82
votes
14 answers

While loop with promises

What would be the idiomatic way to do something like a while loop with promises. So: do something if the condition still stands do it again repeat then do something else. dosomething.then(possilblydomoresomethings).then(finish) I've done it this…
Grummle
  • 1,364
  • 1
  • 12
  • 21
81
votes
3 answers

Django template can't loop defaultdict

import collections data = [ {'firstname': 'John', 'lastname': 'Smith'}, {'firstname': 'Samantha', 'lastname': 'Smith'}, {'firstname': 'shawn', 'lastname': 'Spencer'}, ] new_data = collections.defaultdict(list) for d in data: …
user216171
  • 1,686
  • 3
  • 15
  • 22
81
votes
2 answers

Breaking the nested loop

I'm having problem with nested loop. I have multiple number of posts, and each post has multiple number of images. I want to get total of 5 images from all posts. So I am using nested loop to get the images, and want to break the loop when the…
user1355300
  • 4,867
  • 18
  • 47
  • 71
80
votes
19 answers

For vs. while in C programming?

There are three loops in C: for, while, and do-while. What's the difference between them? For example, it seems nearly all while statements can be replaced by for statements, right? Then, what's the advantage using while?
user355546
  • 825
  • 1
  • 7
  • 3
80
votes
3 answers

How to 'continue' inside a each loop : underscore, node.js

The code in node.js is simple enough. _.each(users, function(u, index) { if (u.superUser === false) { //return false would break //continue? } //Some code }); My question is how can I continue to next index without executing "Some…
user1741851
80
votes
5 answers

Breaking out of a for loop in Java

In my code I have a for loop that iterates through a method of code until it meets the for condition. Is there anyway to break out of this for loop? So if we look at the code below, what if we want to break out of this for loop when we get to…
silverzx
  • 1,209
  • 4
  • 14
  • 18
79
votes
6 answers

Jquery each - Stop loop and return object

Can anybody tell me why the loop did not stop after the 5 entry? http://jsbin.com/ucuqot/edit#preview $(document).ready(function() { someArray = new Array(); someArray[0] = 't5'; someArray[1] = 'z12'; someArray[2] = 'b88'; someArray[3] =…
user970727
  • 1,947
  • 4
  • 22
  • 28
79
votes
3 answers

How to have css3 animation to loop forever

I want to have the whole set of animation to play forever. When the last photo fades off, I want the first one to appear again an so on. What I did (and I dont like) is set the page to reload at the end of the last photo fade out. Is there any other…
CuRSoR
  • 801
  • 1
  • 6
  • 3
79
votes
3 answers

Looping Over Result Sets in MySQL

I am trying to write a stored procedure in MySQL which will perform a somewhat simple select query, and then loop over the results in order to decide whether to perform additional queries, data transformations, or discard the data altogether. …
Dereleased
  • 9,939
  • 3
  • 35
  • 51
78
votes
8 answers

Why can't I use foreach on Java Enumeration?

Why can't I do: Enumeration e = ... for (Object o : e) ...
ripper234
  • 222,824
  • 274
  • 634
  • 905
77
votes
3 answers

Ruby: Continue a loop after catching an exception

Basically, I want to do something like this (in Python, or similar imperative languages): for i in xrange(1, 5): try: do_something_that_might_raise_exceptions(i) except: continue # continue the loop at i = i + 1 How do I…
Santa
  • 11,381
  • 8
  • 51
  • 64
77
votes
9 answers

Skip over a value in the range function in python

What is the pythonic way of looping through a range of numbers and skipping over one value? For example, the range is from 0 to 100 and I would like to skip 50. Edit: Here's the code that I'm using for i in range(0, len(list)): x= listRow(list,…
David
  • 1,398
  • 1
  • 14
  • 20
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
76
votes
13 answers

How can I break out of two nested for loops in Objective-C?

I have two for loops nested like this: for(...) { for(...) { } } I know that there is a break statement. But I am confused about if it breaks both loops or just the one in which it was called? I need to break both ones as soon as I see…
Thanks
  • 40,109
  • 71
  • 208
  • 322
76
votes
2 answers

How to iterate `dict` with `enumerate` and unpack the index, key, and value along with iteration

How to iterate a dict with enumerate such that I could unpack the index, key and value at the time of iteration? Something like: for i, (k, v) in enumerate(mydict): # some stuff I want to iterate through the keys and values in a dictionary…
themink
  • 827
  • 1
  • 6
  • 8