Questions tagged [iteration]

Iterations are the successive repetitions in loops such as for, foreach or while. Questions with this tag are often concerned about how to best handle a collection of data.

Wiki

Definition: Iteration is the repetition of a block of statements within a computer program.

In most computer programming languages, an iteration is a process that allows code to be executed repeatedly based on a given Boolean condition. Loops can be thought of as an iteration statements.

Examples

  • for
  • foreach
  • while
  • do while

Tag usage

The tag can be used for all programming related problems in implementing iterative approach to call a programming code repeatedly. The tag can be used in problems in implementing iteration using programming constructs and loops. Tag can also be used with other tags , , and .

Read more

10795 questions
444
votes
21 answers

Way to go from recursion to iteration

I've used recursion quite a lot on my many years of programming to solve simple problems, but I'm fully aware that sometimes you need iteration due to memory/speed problems. So, sometime in the very far past I went to try and find if there existed…
376
votes
2 answers

In Ruby, how do I skip a loop in a .each loop, similar to 'continue'

In Ruby, how do I skip a loop in a .each loop, similar to continue in other languages?
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
374
votes
13 answers

Is recursion ever faster than looping?

I know that recursion is sometimes a lot cleaner than looping, and I'm not asking anything about when I should use recursion over iteration, I know there are lots of questions about that already. What I'm asking is, is recursion ever faster than a…
Carson Myers
  • 37,678
  • 39
  • 126
  • 176
340
votes
18 answers

How to loop through an array containing objects and access their properties

I want to cycle through the objects contained in an array and change the properties of each one. If I do this: for (var j = 0; j < myArray.length; j++){ console.log(myArray[j]); } The console should bring up every object in the array, right? But…
FlyingLizard
  • 3,521
  • 4
  • 16
  • 10
320
votes
9 answers

Remove elements from collection while iterating

AFAIK, there are two approaches: Iterate over a copy of the collection Use the iterator of the actual collection For instance, List fooListCopy = new ArrayList(fooList); for(Foo foo : fooListCopy){ // modify actual…
user1329572
  • 6,176
  • 5
  • 29
  • 39
316
votes
8 answers

How do I iterate through children elements of a div using jQuery?

I have a div and it has several input elements in it... I'd like to iterate through each of those elements. Ideas?
Shamoon
  • 41,293
  • 91
  • 306
  • 570
305
votes
7 answers

How do I loop through a list by twos?

I want to loop through a Python list and process 2 list items at a time. Something like this in another language: for(int i = 0; i < list.length(); i+=2) { // do something with list[i] and list[i + 1] } What's the best way to accomplish this?
froadie
  • 79,995
  • 75
  • 166
  • 235
297
votes
9 answers

How can I loop through a C++ map of maps?

How can I loop through a std::map in C++? My map is defined as: std::map< std::string, std::map > For example, the above container holds data like this: m["name1"]["value1"] = "data1"; m["name1"]["value2"] =…
Jack
  • 3,769
  • 6
  • 24
  • 32
291
votes
5 answers

How to loop backwards in python?

I'm talking about doing something like: for(i=n; i>=1; --i) { //do something with i } I can think of some ways to do so in python (creating a list of range(1,n+1) and reverse it, using while and --i, ...) but I wondered if there's a more elegant…
snakile
  • 52,936
  • 62
  • 169
  • 241
286
votes
3 answers

Is generator.next() visible in Python 3?

I have a generator that generates a series, for example: def triangle_nums(): '''Generates a series of triangle numbers''' tn = 0 counter = 1 while True: tn += counter yield tn counter += + 1 In Python 2 I am…
jottos
  • 20,098
  • 9
  • 30
  • 27
285
votes
12 answers

How to loop through array in jQuery?

I am trying to loop through an array. I have the following code: var currnt_image_list= '21,32,234,223'; var substr = currnt_image_list.split(','); // array here Am trying to get all the data out of the array. Can some one lead me in the right…
Rickstar
  • 6,057
  • 21
  • 55
  • 74
284
votes
3 answers

How to loop over grouped Pandas dataframe?

DataFrame: c_os_family_ss c_os_major_is l_customer_id_i 0 Windows 7 90418 1 Windows 7 90418 2 Windows 7 90418 Code: print df for name, group in…
Tjorriemorrie
  • 16,818
  • 20
  • 89
  • 131
242
votes
7 answers

Iterate over object attributes in python

I have a python object with several attributes and methods. I want to iterate over object attributes. class my_python_obj(object): attr1='a' attr2='b' attr3='c' def method1(self, etc, etc): #Statements I want to generate…
Pablo Mescher
  • 26,057
  • 6
  • 30
  • 33
234
votes
8 answers

Is there a built-in way to loop through the properties of an object?

Is there a Mustache / Handlebars way of looping through an object properties? So with var o = { bob : 'For sure', roger: 'Unknown', donkey: 'What an ass' } Can I then do something in the template engine that would be equivalent to for(var…
Ben
  • 20,737
  • 12
  • 71
  • 115
231
votes
18 answers

Can every recursion be converted into iteration?

A reddit thread brought up an apparently interesting question: Tail recursive functions can trivially be converted into iterative functions. Other ones, can be transformed by using an explicit stack. Can every recursion be transformed into…
Tordek
  • 10,628
  • 3
  • 36
  • 67