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

9240 questions
5023
votes
40 answers

For-each over an array in JavaScript

How can I loop through all the entries in an array using JavaScript? I thought it was something like this: forEach(instance in theArray) Where theArray is my array, but this seems to be incorrect.
Dante1986
  • 52,129
  • 12
  • 34
  • 53
3555
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
  • 34,445
  • 3
  • 19
  • 19
3532
votes
7 answers

Iterate through a HashMap

What's the best way to iterate over the items in a HashMap?
burntsugar
  • 54,120
  • 21
  • 51
  • 77
2126
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
  • 84,004
  • 11
  • 142
  • 168
1922
votes
27 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
  • 54,286
  • 10
  • 24
  • 20
1238
votes
28 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
  • 206,738
  • 150
  • 445
  • 651
933
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
  • 44,680
  • 16
  • 48
  • 70
621
votes
12 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
  • 9,812
  • 4
  • 34
  • 64
535
votes
9 answers

Iterating each character in a string using Python

In C++, I can iterate over an std::string like this: std::string str = "Hello World!"; for (int i = 0; i < str.length(); ++i) { std::cout << str[i] << std::endl; } How do I iterate over a string in Python?
Paradius
  • 7,389
  • 11
  • 30
  • 36
496
votes
18 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
  • 8,328
  • 10
  • 32
  • 52
488
votes
17 answers

What exactly are iterator, iterable, and iteration?

What is the most basic definition of "iterable", "iterator" and "iteration" in Python? I have read multiple definitions but I am unable to identify the exact meaning as it still won't sink in. Can someone please help me with the 3 definitions in…
thechrishaddad
  • 5,726
  • 7
  • 24
  • 29
458
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
  • 49,211
  • 44
  • 120
  • 177
452
votes
6 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,161
  • 3
  • 17
  • 16
442
votes
24 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
  • 4,958
  • 3
  • 17
  • 14
398
votes
15 answers

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

StringTokenizer? Convert the String to a char[] and iterate over that? Something else?
Paul Wicks
  • 55,730
  • 52
  • 115
  • 144
1
2 3
99 100