Loops are a type of control flow structure in programming in which a series of statements may be executed repeatedly until some condition is met.

learn more… | top users | synonyms (3)

162
votes
10answers
19k views

Javascript closure inside loops - simple practical example

Closures are one of those things which has been discussed a lot on SO, but this situation pops up a lot for me and I'm always left scratching my head what to do. var funcs = {}; for (var i = 0; i ...
20
votes
5answers
4k views

Access outside variable in loop from Javascript closure

See: for (var i in this.items) { var item = this.items[i]; $("#showcasenav").append("<li id=\"showcasebutton_"+item.id+"\"><img src=\"/images/showcase/icon-"+item.id+".png\" ...
24
votes
7answers
4k views

Doesn't JavaScript support closures with local variables?

I am very puzzled about this code: var closures = []; function create() { for (var i = 0; i < 5; i++) { closures[i] = function() { alert("i = " + i); }; } } function run() { ...
12
votes
1answer
7k views

Event handlers inside a Javascript loop - need a closure?

I'm working with a bit of html and Javascript code that I've taken over from someone else. The page reloads a table of data (via an asynchronous request) every ten seconds, and then re-builds the ...
33
votes
6answers
10k views

Speed up the loop operation in R

i have a big performance problem in R. I wrote a function that iterates over an data.frame object. It simply adds a new col to a data.frame and accumulate sth. (simple operation). The data.frame has ...
85
votes
17answers
33k views

Difference between declaring variables before or in loop?

I have always wondered if, in general, declaring a throw-away variable before a loop, as opposed to repeatedly inside the loop, makes any (performance) difference? A (quite pointless example) in ...
16
votes
3answers
9k views

How does a function in a loop (which returns another function) work?

I've been trying to assign a function to onclick event of a dynamically created "a" tag in JavaScript. All of the tags are created in a loop as follows: for ( var i = 0; i < 4; i++ ) { var a = ...
6
votes
5answers
2k views

Please explain the use of JavaScript closures in loops [duplicate]

Possible Duplicate: Javascript closure inside loops - simple practical example I have read a number of explanations about closures and closures inside loops. I have a hard time ...
147
votes
12answers
54k views

Calling remove in foreach loop in Java

In Java, is it legal to call remove on a collection when iterating through the collection using a foreach loop? For instance: List<String> names = .... for (String name : names) { // Do ...
94
votes
8answers
128k views

How to iterate over a JSON structure?

I have the following JSON structure: [ {"id":"10", "class": "child-of-9"}, {"id":"11", "classd": "child-of-10"}]; how to iterate over it using jquery or javascript?
392
votes
3answers
52k views

How foreach actually works

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 ...
63
votes
33answers
13k 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 ...
51
votes
5answers
29k views

do { … } while (0) what is it good for? [duplicate]

Possible Duplicate: Why are there sometimes meaningless do/while and if/else statements in C/C++ macros? I've been seeing that expression for over 10 years now. I've been trying to think ...
72
votes
8answers
5k views

Optimizing away a “while(1);” in C++0x

Updated, see below! I have heard and read that C++0x allows an compiler to print "Hello" for the following snippet #include <iostream> int main() { while(1) ; std::cout << ...
42
votes
11answers
15k views

Is there any overhead to declaring a variable within a loop? (C++)

I am just wondering if there would be any loss of speed or efficiency if you did something like this: int i = 0; while(i < 100) { int var = 4; i++; } which declares int var one hundred ...

1 2 3 4 5 72
15 30 50 per page