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
5616
votes
41 answers

Loop (for each) over an array in JavaScript

How can I loop through all the entries in an array using JavaScript?
Dante1986
  • 58,291
  • 13
  • 39
  • 54
3937
votes
45 answers

How do I efficiently iterate over each entry in a Java Map?

If I have an object implementing the Map interface in Java and I wish to iterate over every pair contained within it, what is the most efficient way of going through the map? Will the ordering of elements depend on the specific map implementation…
iMack
  • 38,281
  • 3
  • 21
  • 19
3748
votes
7 answers

Iterate through a HashMap

What's the best way to iterate over the items in a HashMap?
burntsugar
  • 57,360
  • 21
  • 58
  • 81
2262
votes
7 answers

How does PHP 'foreach' actually work?

Let me prefix this by saying that I know what foreach is, does and how to use it. This question concerns how it works under the bonnet, and I don't want any answers along the lines of "this is how you loop an array with foreach". For a long time I…
DaveRandom
  • 87,921
  • 11
  • 154
  • 174
2062
votes
28 answers

Why is using "for...in" for array iteration a bad idea?

I've been told not to use for...in with arrays in JavaScript. Why not?
lYriCAlsSH
  • 57,436
  • 10
  • 26
  • 20
1295
votes
31 answers

Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop

We all know you can't do the following because of ConcurrentModificationException: for (Object i : l) { if (condition(i)) { l.remove(i); } } But this apparently works sometimes, but not always. Here's some specific code: public…
Claudiu
  • 224,032
  • 165
  • 485
  • 680
932
votes
25 answers

How to remove items from a list while iterating?

I'm iterating over a list of tuples in Python, and am attempting to remove them if they meet certain criteria. for tup in somelist: if determine(tup): code_to_remove_tup What should I use in place of code_to_remove_tup? I can't figure…
lfaraone
  • 49,562
  • 17
  • 52
  • 70
707
votes
13 answers

Ways to iterate over a list in Java

Being somewhat new to the Java language I'm trying to familiarize myself with all the ways (or at least the non-pathological ones) that one might iterate through a list (or perhaps other collections) and the advantages or disadvantages of each.…
jacobq
  • 11,209
  • 4
  • 40
  • 71
625
votes
19 answers

How to iterate over a JavaScript object?

I have an object in JavaScript: { abc: '...', bca: '...', zzz: '...', xxx: '...', ccc: '...', // ... } I want to use a for loop to get its properties. And I want to iterate it in parts (not all object properties at…
nkuhta
  • 10,388
  • 12
  • 41
  • 54
573
votes
9 answers

Iterating each character in a string using Python

How can I iterate over a string in Python (get each character from the string, one at a time, each time through a loop)?
Paradius
  • 7,949
  • 11
  • 32
  • 38
561
votes
15 answers

What are iterator, iterable, and iteration?

What are "iterable", "iterator", and "iteration" in Python? How are they defined? See also: How to build a basic iterator?
thechrishaddad
  • 6,523
  • 7
  • 27
  • 33
537
votes
25 answers

Iterating through a range of dates in Python

I have the following code to do this, but how can I do it better? Right now I think it's better than nested loops, but it starts to get Perl-one-linerish when you have a generator in a list comprehension. day_count = (end_date - start_date).days +…
ShawnMilo
  • 5,896
  • 3
  • 19
  • 15
470
votes
17 answers

What is the easiest/best/most correct way to iterate through the characters of a string in Java?

Some ways to iterate through the characters of a string in Java are: Using StringTokenizer? Converting the String to a char[] and iterating over that. What is the easiest/best/most correct way to iterate?
Paul Wicks
  • 62,960
  • 55
  • 119
  • 146
465
votes
8 answers

How do I iterate over an NSArray?

I'm looking for the standard idiom to iterate over an NSArray. My code needs to be suitable for OS X 10.4+.
Steve McLeod
  • 51,737
  • 47
  • 128
  • 184
464
votes
5 answers

How to skip to next iteration in jQuery.each() util?

I'm trying to iterate through an array of elements. jQuery's documentation says: jquery.Each() documentation Returning non-false is the same as a continue statement in a for loop, it will skip immediately to the next iteration. I've tried calling…
Josh
  • 5,281
  • 3
  • 19
  • 16
1
2 3
99 100