while

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 ) {...

Perl: while ($key = each %hash) doesn't stop at key = 0

I don't need this, obviously; I'm just curious about what's going on here. Am I missing something simple? Can I rely on this behaviour in all versions of Perl?) Perl v5.8.8: %h = ( 0=>'zero', 1=>'one', 2=>'two' ); while ($k = each %h) { $v = delete $h{$k}; print "deleted $v; remaining: @h{0..2}\n"; } outputs deleted one; rem...

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...

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...

Java while loop and Threads!

I have a program that continually polls the database for change in value of some field. It runs in the background and currently uses a while(true) and a sleep() method to set the interval. I am wondering if this is a good practice? And, what could be a more efficient way to implement this? The program is meant to run at all times. Conse...

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...

Is there any way to do variable assignments directly inside a while(<here>) loop in Python?

Is there any way to do this in python? I.e. have the variable assignment return the assigned value and compare that to an empty string, directly in the while loop. No biggie if it isn't possible, just to used to doing it in php. while((name = raw_input("Name: ")) != ''): names.append(name) What I'm trying to do is identical to thi...

Continue in while inside foreach

In the following C# code snippet I have a 'while' loop inside a 'foreach' loop and I wish to jump to the next item in 'foreach' when a certain condition occurs. foreach (string objectName in this.ObjectNames) { // Line to jump to when this.MoveToNextObject is true. this.ExecuteSomeCode(); while (this.boolValue) { ...

stop while statment with messagebox java

I need to find a way to stop a while statement when a messagebox is closed in java. I am modifying a chat program, the server has no gui and listens with a while(true) statement. I am trying to find a way to close the server with out going into task manager and killing java.exe. I have little experience with java so some source code woul...

Python: Mixing files and loops

I'm writing a script that logs errors from another program and restarts the program where it left off when it encounters an error. For whatever reasons, the developers of this program didn't feel it necessary to put this functionality into their program by default. Anyways, the program takes an input file, parses it, and creates an outp...

How is "0" result from readdir() not false in a while condition?

See also: Where in the documentation does it say that while tests readdir for definedness?. (Not a duplicate; just closely related.) Many people treat the loop below as idiomatic: while (defined(my $file = readdir($dir)) { ... } instead of: while (my $file = readdir($dir)) { ... } because supposedly with the latter vers...

Python While Loop Condition Evaluation

Say I have the following loop: i = 0 l = [0, 1, 2, 3] while i < len(l): if something_happens: l.append(something) i += 1 Will the len(i) condition being evaluated in the while loop be updated when something is appended to l? ...

Perl DBI dynamic fetchrow while loops

I'm trying to pass table names to a sub that gets all the field names of that table, stores them into an array, and then uses that array in conjunction with the fetchrow of another sql query to display the data in those fields. Here's the code I have now: Examples of sub calls with table names as the parameter: shamoo("reqhead_rec"); ...

MS Workflow Foundation inheritance and while activity

Hi, I have two questions. 1. Why is workflow class "SEALED" class? Is it a bad practice to inherit workflows? 2. The while activity is slow. IE.: I put 3 activities on a seqential wf in this order... Code_activity1 While_activity Code_activity2 (in the while activity) Code_activity1 - sets an int counter to 33320. While_activity ...

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...

Clojure While Loop

I trying clojure i am trying to figure out how to implement the following algorithm, I am reading from an input stream i want to continue reading until it is not a delimiter character. i can do this in java with a while loop but i can't seem to figure out how to do it in clojure? while read readChar != delimiter do some pr...

What does a while after a print mean in Perl?

I am new to Perl. Know a little bit of C though. I came across this snippet in one of our classroom notes : $STUFF="c:/scripts/stuff.txt"; open STUFF or die "Cannot open $STUFF for read :$!"; print "Line $. is : $_" while (<STUFF>); Why is the while after the print statement? What does it do? ...

While loop combined with header() in PHP

I've written a script to geocode some points which has a structure basically like this: //get an unupdated record $arr_record; while(count($arr_record) > 0) { //strings are derived from $arr_record geocode($string1); geocode($string2); geocode($string3); array_pop($arr_record); } function geocode($string) { //if successful upd...

Problem joining 2 tables

I want to join 2 tables together but i cant get it to work. These are the tables: threads: id, title posts: thread_id, message $sql = mysql_query("SELECT threads.id, threads.title FROM threads JOIN posts ON posts.thread_id = threads.id WHERE threads.id = ".intval($_GET['id']))...

To convert PHP's while loop to a for -loop

How can you convert the following while loop to a for -loop in PHP? while( $row2 = pg_fetch_row( $result_tags ) While -loops are a source of errors for me. I see the while -loop as follows. for ( $i = 0 ; $i < count(pg_fetch_row( $result_tags )) ; $i++ ) ...