loops

Is there ever a need for a "do {...} while ( )" loop?

Bjarne Stroustrup (C++ creator) once said that he avoids "do/while" loops, and prefers to write the code in terms of a "while" loop instead. [See quote below.] Since hearing this, I have found this to be true. What are your thoughts? Is there an example where a "do/while" is much cleaner and easier to understand than if you used a "wh...

Fastest way to loop through a 2d array?

Hey folks, I just stumbled upon this blog post. The author shows two code samples that loop through a rectangle and compute something (my guess is the computing code is just a placeholder). On one of the example, he scans the rectangle vertically, and on the other horizontally. He then says the second is fastest, and every programmer sh...

Optimising jQuery loop

Is there a way of optimising a jQuery loop when dealing with a fairly large amount of data. The browser tends to lock while the data is being looped through, and anything below IE8 gives a 'script running too slow' message. I'm just using a jQuery.each(data, function(i, val) to get the field name and value for the field from the databa...

Loop Through Listbox in VB.net or C# and Set Values to Label Text?

I have items in a listbox control that I would like to repeatedly (when it gets to the last one, repeat) loop through and set the text to a label. I'm stuck, please help! ...

getchar() question

Hi, this is such a newbie question that I'm almost embarrassed to ask it :$ ... but anyway: I've started reading "The C Programming Language" (K&R) and I have a doubt about the getchar() function. For example this code: #include <stdio.h> main(){ int c; c = getchar(); putchar(c); printf("\n"); } given the entry "toomanyc...

C#: Cleanest way to divide a string array into N instances N items long

I know how to do this in an ugly way, but am wondering if there is a more elegant and succinct method. I have a string array of e-mail addresses. Assume the string array is of arbitrary length -- it could have a few items or it could have a great many items. I want to build another string consisting of say, 50 email addresses from the ...

Mac Event Loop QT Plugin in a NON QT Application

I am trying to write a Trolltech QT library that will be used from a NON-QT CFM Application written in MacApp. I am having trouble that when I have created QApplication it is taking over my event loop in my non-Qt MacApp mac application. I have override QApplication::macEventFilter to call CallNextEventHandler but this calls my Carbon Ev...

What are your tips for keeping track and avoiding bugs in loops ?

I just found ... AGAIN ... a real time wastage bug as follows for (int i = 0; i < length; i++) { //...Lots of code for (int j = 0; i < length; j++) { //...Lots of code } } Did you notice straight ahead the inner i which SHOULD BE j ? Neither did I. So from now on I am going to use: for (int i = 0; i < length; i...

Iterating through the Alphabet - C# a-caz

Hi i have question about iterate throught the Alphabet. I would like to have a loop that begins with "a" and ends with "z". After that the loop begins "aa" and count to "az". after that begins with "ba" up to "bz" and so on... Anybody know some solution? Thanks EDIT: I forgot that i give an char "a" to the function then the function m...

Python - Previous and next values inside a loop

How can I do thing like this in python? foo = somevalue previous = next = 0 for (i=1; i<objects.length(); i++) { if (objects[i]==foo){ previous = objects[i-1] next = objects[i+1] } } ...

Python behavior of string in loop

In trying to capitalize a string at separators I encountered behavior I do not understand. Can someone please explain why the string s in reverted during the loop? Thanks. s = 'these-three_words' seperators = ('-','_') for sep in seperators: s = sep.join([i.capitalize() for i in s.split(sep)]) print s print s stdo...

Where inside injected DLL to loop?

Hey guys, So I've got an application that starts another application with my DLL injected (with Detours). The entry point is DllMain. I can't do much from DllMain, and certainly cannot loop. So how do I call my DLL monitor functions every x seconds? I read you cannot create a thread from DllMain (at least until it returns) and its true ...

Applescript equivalent of "continue"?

Hi, I have a simple 'repeat with' in an AppleScript, and would like to move on to the next item in the "repeat" conditionally. Basically I'm looking for something similar to "continue" (or break?) in other languages. I'm not well versed in AppleScript but I have found it useful a few times now. ...

Rails: An elegant way to display a message when there are no elements in database.

Hello, I realized that I'm writing a lot of code similar to this one: <% unless @messages.blank? %> <% @messages.each do |message| %> <%# code or partial to display the message %> <% end %> <% else %> You have no messages. <% end %> Is there any construct in Ruby and/or Rails that would let me skip that first condition? So...

How can I access variables that are named sequentially by a loop while still inside of the loop?

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the variable $tab) (23:32:12\[[email protected]) [~/bin]$ ...

When is a do-while appropriate?

When is a do-while the better choice over other types of loops? What are some common scenarios where its better than others? I understand the function of a do-while, but not when to use it. ...

loop through JPanel

Hi there, In order to initialise all JTextfFields on a JPanlel when users click a "clear button" I need to loop through the JPanel (instead of setting all individual field to ""). Can someone please show me how to use a For Each loop in order to iterate thru the JPanel in search of JTextFields. Very much appreciated Dallag. ...

Why can't I roll a loop in Javascript?

I am working on a web page that uses dojo and has a number (6 in my test case, but variable in general) of project widgets on it. I'm invoking dojo.addOnLoad(init), and in my init() function I have these lines: dojo.connect(dijit.byId("project" + 0).InputNode, "onChange", function() {makeMatch(0);}); dojo.connect(dijit.byId("project" ...

Loop through multi dimension array to show grid in PHP

I have 3 tables. Apls, Hulls and AplsHulls. Apls consists of id, name, date Hulls consists of id, production_name AplsHulls is a join table and consists of id, apl_id, hull_id, status. Not every Hull is associated with each Apl. The ones that are are in the join table with a status (shipped,in production, etc.) I need to display a rep...

Cannot collect alle results of asynchronous call with geo.getLocations (Google Maps)

Below is my code. The problem is that recordsOut[0] is always undefined, whatever I try. I know it has to do with the callback result. I tried to add some delays to give it more time to give a result back, but that did not help. Any idea (with example please)? Very much appreciated. function getAddress(id, searchValue) { geo.getLo...