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
5306votes
40answers
4.8mviews

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.
user avatar
  • 55.1k
4599votes
22answers
3.1mviews

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 ...
user avatar
  • 289k
4159votes
33answers
918kviews

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() { ...
user avatar
  • 232k
3687votes
44answers
4.6mviews

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 ...
user avatar
3649votes
7answers
4.2mviews

Iterate through a HashMap [duplicate]

What's the best way to iterate over the items in a HashMap?
user avatar
  • 55.7k
3345votes
44answers
2.5mviews

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 ...
user avatar
  • 41.6k
3097votes
44answers
445kviews

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); ...
user avatar
  • 516k
2998votes
30answers
2.0mviews

How to iterate over a dictionary?

I've seen a few different ways to iterate over a dictionary in C#. Is there a standard way?
user avatar
  • 30.2k
2905votes
3answers
248kviews

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 ...
user avatar
  • 25.8k
2261votes
31answers
1.5mviews

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]); } ...
user avatar
  • 23k
2194votes
7answers
438kviews

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 ...
user avatar
  • 85.9k
1983votes
27answers
940kviews

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?
user avatar
  • 55.8k
1980votes
36answers
1.3mviews

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; // Breaks ...
user avatar
  • 23.4k
1792votes
16answers
2.2mviews

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: ...
user avatar
1745votes
27answers
1.9mviews

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&...
user avatar
  • 21.2k

15 30 50 per page
1
2 3 4 5
5879