loops

Loops and Garbage Collection

I am working on a web application and I have run into the following situation. Dim a as Object Dim i as Integer = 0 Try For i=1 to 5 a = new Object() 'Do stuff ' a = Nothing Next Catch Finally a = Nothing End Try Do i need to do the a=Nothing in the loop or will the garbage collector clean ...

Best way to break from nested loops in Javascript?

What's the best way to break from nested loops in Javascript? //Write the links to the page. for (var x = 0; x < Args.length; x++) { for (var Heading in Navigation.Headings) { for (var Item in Navigation.Headings[Heading]) { if (Args[x] == Navigation.Headings[Heading][Item].Name) { documen...

What is the best way to do loops in JavaScript

I have stumbled into several methods of looping in JavaScript, what I like the most is: for(var i = 0; i < a.length; i++){ var element = a[i]; } But as tested here (http://www.robertnyman.com/2008/04/11/javascript-loop-performance/), it should probably be written so that the length is only calculated once. In jQuery there is a ....

recursion instead of multi-loops

I want this method to work for any given number of arguments, i can do that with code generation(with a lot of ugly code), can it be done with recursion? if so how? I understand recursion, but i dont know how to write this. private static void allCombinations(List<String>... lists) { if (lists.length == 3) { for (String s3 : lists[0]...

Remove repetitive, hard coded loops and conditions in C#

I have a class that compares 2 instances of the same objects, and generates a list of their differences. This is done by looping through the key collections and filling a set of other collections with a list of what has changed (this may make more sense after viewing the code below). This works, and generates an object that lets me kno...

Iterating through list and creating summary lines on the fly

EDIT: I missed a crucial point: .NET 2.0 Consider the case where I have a list of unsorted items, for the sake of simplicity of a type like this: class TestClass { DateTime SomeTime; decimal SomePrice; // constructor } I need to create a report-like output, where the total prices for each day are accumulated. There shoul...

Simple Model Checker Tool

Is there a simple Model Checker tool. I am planning to implement a model checker tool which will analyze the code for some of the predefined properties. ...

Test loops at the top or bottom? (while vs. do while)

When I was taking CS in college (mid 80's), one of the ideas that was constantly repeated was to always write loops which test at the top (while...) rather than at the bottom (do ... while) of the loop. These notions were often backed up with references to studies which showed that loops which tested at the top were statistically much mo...

foreach vs someList.Foreach(){}

There are apparently many ways to iterate over a collection. Curious if there are any differences, or why you'd use one way over the other. First type: List<string> someList = <some way to init> foreach(string s in someList) { <process the string> } Other Way: List<string> someList = <some way to init> someList.ForEach(delegate(s...

Replacing array in foreach loop with array modified in same loop

foreach($arrayOne as $value){ do function } In the above example, I'd like to pass $arrayOne into a loop, have a function operate that removes some elements of $arrayOne and then have the loop pass over the reduced $arrayOne on the elements that are left until the loop returns false. Recommendations? ...

Loop from 0x0000 to 0xFFFF

I'd like a loop that uses a UInt16 (ushort) to loop through all of its values. However, this doesn't do it: for( ushort i = 0; i < UInt16.MaxValue; i++ ) { // do something } The problem is that the loop will quit when i == 0xFFFF and not "do something". If I change the 'for' statement to "for(ushort i = 0; i <= UInt16.MaxValue; i...

How to run a loop of queries in access?

Hi I have a database with a table which is full of conditions and error messages for checking another database. I want to run a loop such that each of these conditions is checked against all the tables in the second database and generae a report which gives the errors. Is this possible in ms access. For example, querycrit table id...

PHP: Right way to declare variable before use in loop

I have a variable that is built in loop. Something like: $str = ""; for($i = 0; $i < 10; $i++) $str .= "something"; If $str = "" is ommitted, I get undefined variable notice, but I thought php auto-declare a variable the first time it sees undeclared one? How do I do this right? ...

Best refactoring for the dreaded While (True) loop

If, like me, you shiver at the site of a While (True) loop, then you too must have thought long and hard about the best way to refactor it away. I've seen several different implementations, none really better than any other, such as the timer & delegate combination. So what's the best way you've come up with or seen to refactor the dre...

Can you enumerate a collection in C# out of order?

Is there a way to use a foreach loop to iterate through a collection backwards or in a completely random order? ...

do { ... } while (0) what is it good for?

Hey! I've been seeing that expression for over 10 years now. I've been trying to think what it's good for. Since I see it mostly in #defines, I assume it's good for inner scope variable declaration and for using breaks (instead of gotos.) Is it good for anything else? Do you use it? ...

openbd cfloop over a date

I'm trying to convert my sites from CF8 to openBD. I have a cfloop in a site that loops over a date range. In essence, I want to insert a new record into the db for every 2 weeks (step) of a date range (from and to) my loop looks like this... <cfloop from = "#form.startDate#" to = "#form.endDate#" index = "i" step =...

Are loops the best way to build a table?

I have to build an HTML table that shows data for users versus pages visited. It seems klunky to use for and/or foreach loops, but I can't think of anything better. I'm using PHP, but I would assume that this is language agnostic. ...

python, basic question on loops

It's a really basic question but i can't think at the second. How do i set up a loop that asks each time the function inside runs whether to do it again. So it runs it then says something like; "loop again? y/n" ...

How do I make for loops run side by side?

I have been working on a childish little program: there are a bunch of little circles on the screen, of different colors and sizes. When a larger circle encounters a smaller circle it eats the smaller circle, and when a circle has eaten enough other circles it reproduces. It's kind of neat! However, the way I have it implemented, the pr...