loops

VB.NET..... Loop help

Hi, I am using My.Settings in visual studio 2008 to store information, for when the user runs the program again. I have that working fine... but as I am using 12 textboxes I don't want to write... my.settings.grade1 = textbox1.text for each one, and I am also making calculations using the stored information, so I dont want to be writi...

Calling fellow code nerds - Alternatives to Nested Loops?

It is not uncommon for me (or likely anyone else) to have a list of objects I need to iterate through and then interact with a list of properties. I use a nested loop, like this: IList<T> listOfObjects; IList<TProperty> listOfProperties; foreach (T dataObject in listOfObjects) { foreach (TProperty property in listOfProperties) ...

Algorithm: iterate over 2 variables in order of descending product

Note: This is NOT homework. Hi, It's easy to iterate over two variables and print their product like so: for a in range(1,100): for b in range(1,100): # or range(a,100) to prevent duplicates print( "%s = %s * %s" % (a*b,a,b) ) However, is it possible to come up with a looping structure that will iterate over a and b in desc...

While vs. Do While

I've seen both the blocks of code in use several different times, personally I have always used the first but my question is: is there a functional difference, and if there is what is it? while (condition is true ) { // do something } do { // do something } while ( condition is true); I will be applying this to PHP but I assu...

Invalid token 'while' in class, struct, or interface member declaration in very simple code

Hi there, I am not sure what the problem is but I keep receiving this error when I try to use a while statement in my code. Invalid token 'while' in class, struct, or interface member declaration I want to use a while loop to have something continuously update while a statement is true. The rest of my code is rather long but w...

Looping in a spiral

A friend was in need of an algorithm that would let him loop through the elements of an NxM matrix (N and M are odd). I came up with a solution, but I wanted to see if my fellow SO'ers could come up with a better solution. I'm posting my solution as an answer to this question. Example Output: For a 3x3 matrix, the output should be: (...

.NET Rectangular Arrays: how to access in a loop?

Basically you have two ways for doing this: for (int x = 0; x < UPPER_X; x++) for (int y = 0; y < UPPER_Y; y++) { arr1[x, y] = get_value(); arr2[y, x] = get_value(); } The only difference is what variable to change in the inner loop: first or second. I heard that the results differ from language to ...

Difference between declaring variables before or in loop?

Hi, 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 Java: a) declaration before loop: double intermediateResult; for(int i=0;i<1000;i++){ intermediateResult = i; System.out.println...

How to vectorize with gcc?

The v4 series of the gcc compiler can automatically vectorize loops using the SIMD processor some modern CPUs, such as the AMD Athlon or Intel Pentium/Core chips. How is this done? ...

What compilers besides gcc can vectorize code?

GCC can vectorize loops automatically when certain options are specified and given the right conditions. Are there other compilers widely available that can do the same? ...

Practical use of automatic vectorization?

Has anyone taken advantage of the automatic vectorization that gcc can do? In the real world (as opposed to example code)? Does it take restructuring of existing code to take advantage? Are there a significant number of cases in any production code that can be vectorized this way? ...

Auto-vectorizing vs. vectorized code by hand

Is it better in some sense to vectorize code by hand, using explicit pragmas or to rely on or use auto-vectorization? For optimum performance using auto-vectorization, one would have to monitor the compiler output to ensure that loops are being vectorized or modify them until they are vectorizable. With hand coding, one is certain th...

Is it possible to exit a for before time in C++, if an ending condition is reached?

Hello, I want to know if it is possible to end a for loop in C++ when an ending condition (different from the reacheing right number of iterations) is verified. For instance: for (int i = 0; i < maxi; ++i) for (int j = 0; j < maxj; ++j) // But if i == 4 < maxi AND j == 3 < maxj, // then jump out of the two nested lo...

Is there any way to do n-level nested loops in Java?

In other words, can I do something like for() { for { for { } } } Except N times? In other words, when the method creating the loops is called, it is given some parameter N, and the method would then create N of these loops nested one in another? Of course, the idea is that there should be an "easy" or "the usua...

In Complexity Analysis why is ++ considered to be 2 operations?

In my Computer Science II class, the professor considers ++,--,*=, etc. to be 2 operations. However, at the Assembly level this is not really two operations. Can someone explain or is this just for the sake of simplicity? ...

How to know if a MFC message loop is already running?

Is there any way to know whether a MFC message loop is already running? EDIT: Context: A library (with event handling) needs to know whether its event filtering has to attach to an existing MFC message loop or create its own message loop: in case a main message loop already exists it must not create its own loop because it would be bloc...

What's the most efficient way to access sibling dictionary value in a Python dict?

In Python, I've got a list if dictionaries that looks like this: matchings = [ {'id': 'someid1', 'domain': 'somedomain1.com'}, {'id': 'someid2', 'domain': 'somedomain2.com'}, {'id': 'someid3', 'domain': 'somedomain3.com'} ] and, I have a variable: the_id = 'someid3' What's the most efficient way to retrieve the domain v...

How to tell a lambda function to capture a copy instead of a reference in C#?

I've been learning C#, and I'm trying to understand lambdas. In this sample below, it prints out 10 ten times. class Program { delegate void Action(); static void Main(string[] args) { List<Action> actions = new List<Action>(); for (int i = 0; i < 10; ++i ) actions.Add(()=>Console.WriteLine(i)); foreach (Action a in actio...

Why are we using i as a counter in loops

why are we using for (int i = 0 ; i < count ; i++){ } why the i why not for (int a = 0; a < count; a++){ } I do it, you do it, everyone does it but WHY? *edit I found out an old saying about FORTRAN which is more funny than correct which says "god is real, everything else above is an integer". "god" would be a variable name s...

When should I use IEnumerator for looping in c#?

I was wondering if there are any times where it's advantageous to use an IEnumerator over a foreach loop for iterating through a collection? For example, is there any time where it would be better to use either of the following code samples over the other? IEnumerator<MyClass> classesEnum = myClasses.GetEnumerator(); while(classesEnum.M...