We changed our privacy policy. Read more.

Questions tagged [loops]

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.

Filter by
Sorted by
Tagged with
5145
votes
40answers
4.6m views

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.
4328
votes
23answers
2.7m views

Accessing the index in 'for' loops?

How do I access the index in a for loop like the following? ints = [8, 23, 45, 12, 78] for i in ints: print('item #{} = {}'.format(???, i)) I want to get this output: item #1 = 8 item #2 = 23 ...
4076
votes
32answers
879k views

How to enumerate an enum

How can you enumerate an enum in C#? E.g. the following code does not compile: public enum Suit { Spades, Hearts, Clubs, Diamonds } public void EnumerateAllSuitsDemoMethod() { ...
3583
votes
7answers
4.1m views

Iterate through a HashMap [duplicate]

What's the best way to iterate over the items in a HashMap?
3559
votes
43answers
4.3m views

Loop through an array in JavaScript

In Java you can use a for loop to traverse objects in an array as follows: String[] myStringArray = {"Hello", "World"}; for (String s : myStringArray) { // Do something } Can you do the same in ...
3252
votes
42answers
2.3m views

How do I loop through or enumerate a JavaScript object?

I have a JavaScript object like the following: var p = { "p1": "value1", "p2": "value2", "p3": "value3" }; Now I want to loop through all p elements (p1, p2, p3...) And get their keys ...
3043
votes
44answers
429k views

JavaScript closure inside loops – simple practical example

var funcs = []; // let's create 3 functions for (var i = 0; i < 3; i++) { // and store them in funcs funcs[i] = function() { // each should log its value. console.log("My value: " + i); ...
2910
votes
30answers
1.9m views

What is the best way to iterate over a dictionary? [closed]

I've seen a few different ways to iterate over a dictionary in C#. Is there a standard way?
2867
votes
3answers
245k views

Why is printing "B" dramatically slower than printing "#"?

I generated two matrices of 1000 x 1000: First Matrix: O and #. Second Matrix: O and B. Using the following code, the first matrix took 8.52 seconds to complete: Random r = new Random(); for (int i ...
2220
votes
30answers
1.5m views

Iterate through object properties

var obj = { name: "Simon", age: "20", clothing: { style: "simple", hipster: false } } for(var propt in obj){ console.log(propt + ': ' + obj[propt]); } ...
2154
votes
7answers
431k views

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 ...
1960
votes
27answers
910k views

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?
1948
votes
36answers
1.3m views

How do I break out of nested loops in Java?

I've got a nested loop construct like this: for (Type type : types) { for (Type t : types2) { if (some condition) { // Do something and break... break; // ...
1709
votes
26answers
1.8m views

How to loop through a plain JavaScript object with the objects as members

How can I loop through all members in a JavaScript object, including values that are objects? For example, how could I loop through this (accessing the "your_name" and "your_message&...
1687
votes
15answers
2.0m views

Looping through the content of a file in Bash

How do I iterate through each line of a text file with Bash? With this script: echo "Start!" for p in (peptides.txt) do echo "${p}" done I get this output on the screen: Start! ./runPep.sh: ...

15 30 50 per page
1
2 3 4 5
5628