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
3
votes
4 answers

Is there a way to fill a collection using a LINQ expression?

One of the great things about LINQ is that allows you to get information that's related to a collection without having to manually write code to iterate through the collection. Is there a way to use LINQ to populate a collection, thus avoiding the…
Ben McCormack
  • 32,086
  • 48
  • 148
  • 223
3
votes
3 answers

How to control a loop using a button?

I am a beginner in java, I want to know is there any possible way to control a loop by clicking a button? I am creating a GUI, and it's supposed to run 10 times in the loop. Is there a way that I could have a button on the screen so that when the…
Daniel
  • 319
  • 2
  • 4
  • 12
3
votes
2 answers

Sort a deeply nested tuple

I have a list of nested tuples that looks like: l = [('apple', ['gala','fuji', 'macintosh']), ('pear', ['seckel','anjou','bosc'])] And I like to sort the second item of the tuple alphabetically, so that it would look like: l2 = [('apple',…
user3008918
  • 105
  • 1
  • 7
3
votes
5 answers

Fastest way to iterate a List in the order defined by an int[]

What ist the fastest way to iterate a list of elements where each item has an associated "score" and items with hightest score come first. Example: List items = new List(new X[]{a,b,c,d}); int[] score = new int[]{20,301,-34,7}; foreach(X x in…
Danvil
  • 22,240
  • 19
  • 65
  • 88
3
votes
4 answers

Convert function from recursion to iteration

I have this function that I wrote that is abysmally slow since php does not handle recursion well. I am trying to convert it to a while loop, but am having trouble wrapping my head around how to do it. Can anyone give me some tips? public…
Eric Conner
  • 10,422
  • 6
  • 51
  • 67
3
votes
2 answers

How to Calculate Dry and Wet Spell in Python?

I have a random time series data with four columns like: year, month, day, precipitation. I want to calculate dry/wet spell for different spell-length. I am looking for a more convenient way to do that while currently doing with some ugly codes like…
PyLabour
  • 245
  • 3
  • 5
  • 15
3
votes
2 answers

Iterative hashing

I'm just wondering, is there a reason why some libraries (be it any language) use iterative hashing such that the hashed data is encoded in hex and rehashed again instead of rehashing the actual binary output?
Tower
  • 98,741
  • 129
  • 357
  • 507
3
votes
2 answers

Is a Recursive-Iterative Method Better than a Purely Iterative Method to find out if a number is prime?

I made this program in C that tests if a number is prime. I'm as yet unfamiliar with Algorithm complexity and all that Big O stuff, so I'm unsure if my approach, which is a combination of iteration and recursion, is actually more efficient than…
Vincent
  • 319
  • 1
  • 11
3
votes
4 answers

Loop through double quotes but ignore double quotes in single quotes

I'm having what I guess is a logic problem. I'm coding in C# but a general pseudo-code solution is welcome. I have this text files that contain, for example, this text: blah "hello john" blah 'the code is "flower" ' blah "good night" I would like…
Juicy
  • 11,840
  • 35
  • 123
  • 212
3
votes
2 answers

Breadth first iteration?

If we wish to cover a search space, like say for all triplets (x, y, z), where x, y, and z are between 1 and n, we can use nested looping to do this: for (int x = 1; x <= n; x++) for (int y = 1; y <= n; y++) for (int z = 1; z <= n;…
Dave Cousineau
  • 12,154
  • 8
  • 64
  • 80
3
votes
4 answers

How to iterate over a string using a buffer (python)

I'm trying to find some code that, given a string, will allow me to iterate over each line using the for loop construct, but with the added requirement that separate for loop constructs will not reset the iteration back to the beginning. At the…
Trent
  • 2,328
  • 3
  • 33
  • 51
3
votes
2 answers

add a short delay between each iteration of do while loop

I have written a code to trigger click event on the four labels on my page and the loop is working fine as i tested it using alert box written in loop body. but by default, in jquery all iterations happen so quick that it is impossible to see those…
tushar.dahiwale
  • 146
  • 2
  • 13
3
votes
1 answer

JasperReports: iterating List inside JRXML

In one of my report requirements, I need to show multiple rows with customized data. For customized data, I used REPORT_SCRIPTLET feature which is filling a List with data objects. Till this point everything works. How can I pass the above filled…
rawat
  • 165
  • 1
  • 2
  • 15
3
votes
3 answers

Converting for loops to while loops in python

I am struggling to find an efficient way to convert these for loops to a working set of while loops. Any Suggestions? I am using 2.7 def printTTriangle(height): for row in range(1,height+1): # print row T's for col in range(1,row+1): …
Greg Jennings
  • 53
  • 1
  • 2
  • 6
3
votes
2 answers

Python: Modifying one attribute to each object in a list

My question is basic and is maybe a duplicate but I haven't found the answer. I am looking for the most efficient (pythonic) way of adding 1 to the attribute x of all objects of a given list. class Any(object): def __init__(self, x): …
Remi.b
  • 17,389
  • 28
  • 87
  • 168