loops

C# DataTable Loop Performance

Which of the following has the best performance? I have seen method two implemented in JavaScript with huge performance gains, however, I was unable to measure any gain in C# and was wondering if the compiler already does method 2 even when written like method 1. The theory behind method 2 is that the code doesn't have to access DataTa...

'Break' equivalent keyword for VB

Just moved over to the VB team here at work. Quick easy one for my 1st question. What is the equivalent keyword to break in VB, i.e., to exit a loop early but not the method? Cheers! ...

C# loop - break vs. continue

In a C# (feel free to answer for other languages) loop, what's the difference between break and continue as a means to leave the structure of the loop, and go to the next iteration? Example: foreach (DataRow row in myTable.Rows){ if (someConditionEvalsToTrue) { break; //what's the difference between this and continue ? ...

Replacement for for... if array iteration

I love list comprehensions in Python, because they concisely represent a transformation of a list. However, in other languages, I frequently find myself writing something along the lines of: foreach (int x in intArray) if (x > 3) //generic condition on x x++ //do other processing This example is in C#, where I'm under the ...

The difference between loops

It's about PHP but I've no doubt many of the same comments will apply to other languages. Simply put, what are the differences in the different types of loop for PHP? Is one faster/better than the others or should I simply put in the most readable loop? for ($i = 0; $i < 10; $i++) { # code... } foreach ($array as $index => $value) {...

Difference between foreach and for loops over an IEnumerable class in C#

I have been told that there is a performance difference between the following code blocks. foreach (Entity e in entityList) { .... } and for (int i=0; i<entityList.Count; i++) { Entity e = (Entity)entityList[i]; ... } where List<Entity> entityList; I am no CLR expect but from what I can tell they should boil down to basi...

How do I build a loop in JavaScript?

How can I build a loop in JavaScript? ...

Continue Considered Harmful?

Should developers avoid using continue in C# or its equivalent in other languages to force the next iteration of a loop? Would arguments for or against overlap with arguments about Goto? ...

How can I create a loop in an onClick event?

I want to write an onClick event which submits a form several times, iterating through selected items in a multi-select field, submitting once for each. How do I code the loop? I'm working in Ruby on Rails and using remote_function() to generate the javascript for the ajax call. ...

Is there a way to loop through a table variable in TSQL without using a cursor?

Let's say I have the following simple table variable: declare @databases table ( DatabaseID int, Name varchar(15), Server varchar(15) ) -- insert a bunch rows into @databases Is declaring and using a cursor my only option if I wanted to iterate through the rows? Is there another way? ...

Iterate over and check the byte value of every character in a string - VBA

Iterate over and check the byte value of every character in a string - VBA Code I have: cell_val = CStr(Nz(fld.value, "")) Dim iter As Long For iter = 0 To Len(cell_val) - 1 Step 1 If Asc(Mid(cell_val, iter, 1)) > 127 Then addlog "Export contains ascii character > 127" End If ...

Counter inside xsl:for-each loop

How to get a counter inside xsl:for-each loop that would reflect the number of current element processed. For example my source XML is <books> <book> <title>The Unbearable Lightness of Being </title> </book> <book> <title>Narcissus and Goldmund</title> </book> <book> <title>Choke</title> </book> </...

What is the difference between range and xrange?

Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it is faster) or what besides that is different about for i in range(0, 20): for i in xrange(0, 20): ...

Is there a "do ... while" loop in Ruby?

I'm using this code to let the user enter in names while the program stores them in an array until they enter an empty string (they must press enter after each name): people = [] info = 'a' # must fill variable with something, otherwise loop won't execute while not info.empty? info = gets.chomp people += [Person.new(info)] if n...

How to find the foreach index

Hi all, Is it possible to find the foreach index? in a "for" loop as follows: for($i = 0; $i < 10; ++$i){ echo $i.' '; } $i will give you the index. Do I have to use the for loop or is there some way to get the index in the foreach loop? ...

Should try...catch go inside or outside a loop?

I have a loop that looks something like this: for(int i = 0; i < max; i++) { String myString = ...; float myNum = Float.parseFloat(myString); myFloats[i] = myNum; } This is the main content of a method whose sole purpose is to return the array of floats. I want this method to return null if there is an error, so I put the ...

flash: for loops running slow

I have a question about loops in flash.... In a tile game I'm making a have a mini map of the whole level. The way it renders the map is a function with a for loop in another for loop. It cycles through each tile position and attaches a map piece (basically a 3x3 pixel square) which is colored according to what the tile is. Anyway, my p...

Does the last element in a loop deserve a separate treatment?

When reviewing, I sometimes encounter this kind of loop: i = begin while ( i != end ) { // ... do stuff if ( i == end-1 (the one-but-last element) ) { ... do other stuff } increment i } Then I ask the question: would you write this? i = begin mid = ( end - begin ) / 2 // (the middle element) while ( i != end ) {...

What's the best way to loop through a set of elements in JavaScript?

In the past and with most my current projects I tend to use a for loop like this: var elements = document.getElementsByTagName('div'); for (var i=0; i<elements.length; i++) { doSomething(elements[i]); } I've heard that using a "reverse while" loop is quicker but I have no real way to confirm this: var elements = document.getEleme...

ForEach loop in Mathematica.

I'd like something like this: ForEach[i_, {1,2,3}, Print[i] ] Or, more generally, to destructure arbitrary stuff in the list you're looping over, like: ForEach[{i_, j_}, {{1,10}, {2,20}, {3,30}}, Print[i*j] ] (Meta-question: is that a good way to call a ForEach loop, with the first argument a pattern like that?) ADDED: Some a...