A for loop is a control structure used by many programming languages to iterate over a range. Depending on the language this may be over a range of integers, iterators, etc.
362
votes
16answers
169k views
Why is using “for…in” with array iteration such a bad idea?
I've been told not to use "for...in" with Arrays in JavaScript but never received a satisfactory reason as to why not.
80
votes
7answers
27k views
Elements order in a “for (… in …)” loop
Does the "for…in" loop in Javascript loop through the hashtables/elements in the order they are declared? Is there a browser which doesn't do it in order?
The object I wish to use will be declared ...
318
votes
13answers
336k views
Loop through array in JavaScript
In Java you can use a for() loop to go through objects in an array like so:
String[] myStringArray = {"Hello","World"};
for(String s : myStringArray)
{
//Do something
}
can you do the same in ...
160
votes
14answers
109k views
What is the difference between ++i and i++
In C, what is the difference between using ++i and i++, and which should be used in the incrementation block of a for loop?
77
votes
32answers
33k views
In .NET, which loop runs faster, 'for' or 'foreach'?
In C#/VB.NET/.NET, which loop runs faster, for or foreach?
Ever since I read that a for loop works faster than a foreach loop a long time ago I assumed it stood true for all collections, generic ...
52
votes
10answers
20k views
Is there a performance difference between a for loop and a for-each loop?
What, if any, is the performance difference between the following two loops?
for(Object o: objectArrayList){
o.DoSomthing();
}
and
for(int i=0; i<objectArrayList.size(); i++){
...
69
votes
5answers
26k views
Performance difference for control structures 'for' and 'foreach' in C#
Which code snippet will give better performance? The below code segments were written in C#.
1.
for(int counter=0; counter<list.Count; counter++)
{
list[counter].DoSomething();
}
2.
...
71
votes
33answers
14k views
JavaScript - Are loops really faster in reverse…?
I've heard this quite a few times. Are JavaScript loops really faster when counting backward? If so, why? I've seen a few test suite examples showing that reversed loops are quicker, but I can't find ...
57
votes
14answers
31k views
Breaking out of a nested loop
If I have a for loop which is nested within another, how can I efficiently come out of both loops (inner and outer) in the quickest possible way?
I don't want to have to use a boolean and then have ...
151
votes
7answers
6k views
Why does the order of the loops affect performance when iterating over a 2D array? [duplicate]
Possible Duplicate:
Which of these two for loops is more efficient in terms of time and cache performance
Below are two programs that are almost identical except that I switched the i and j ...
60
votes
9answers
24k views
Is there a way to access an iteration-counter in Java's for-each loop?
Is there a way in Java's for-each loop
for(String s : stringArray) {
doSomethingWith(s);
}
to find out how often the loop has already been processed?
Aside from using using the old and ...
17
votes
2answers
4k views
Merge several data.frames into one data.frame with a loop
I am trying to merge several data.frames into one data.frame. Since I have a whole list of files I am trying to do it with a loop structure.
So far the loop approach works fine. However, it looks ...
16
votes
3answers
3k views
Type Mismatch on Scala For Comprehension
I don't understand why this construction causes a Type Mismatch error in Scala:
for (first <- Some(1); second <- List(1,2,3)) yield (first,second)
<console>:6: error: type mismatch;
...
30
votes
3answers
11k views
Performance of FOR vs FOREACH in PHP
First of all, I understand in 90% of applications the performance difference is completely irrelevant, but I just need to know which is the faster construct. That and...
The information currently ...
36
votes
9answers
2k views
Which of these two for loops is more efficient in terms of time and cache performance
Which of the following samples of code is more efficient in terms of cache performance? Why?
int a[100][100];
for(i=0; i<100; i++)
{
for(j=0; j<100; j++)
{
a[i][j] = 10;
}
...