while-loops

When should I use do-while instead of while loops?

Possible Duplicates: Is there ever a need for a do {} while ( ) loop? Do your loops test at the top or bottom? While vs. Do While When is a do-while appropriate? I've been having a philosophical debate with a colleague as to whether as a general rule, in C/C++ we should should use while loops rather than do-wile loops. ...

Could someone explain me this while loop for PHP?

Could some one explain me this while loop? while (list($key, $value) = each($HTTP_GET_VARS)) { $get_url .= $key . '=' . $value . '&'; } I know its silly but many times silly things makes huge difference.... ...

Best Loop Idiom for special casing the last element

I run into this case a lot of times when doing simple text processing and print statements where I am looping over a collection and I want to special case the last element (for example every normal element will be comma separated except for the last case). Is there some best practice idiom or elegant form that doesn't require duplicatin...

[PHP] Using a Loop Inside a Function

Hello, I'm still learning functions and how they work. I know what I'm doing wrong just not how to fix it. I am writing a function to pull image data out of a database and return it onto the screen. It works but if there is more than one image it will return the last image only. I know the problem is that $project_image is only returni...

Appropriate use of Multi-level Break in Java

I was recently coding a small java program (as design for an 8086 Assembler program) and I wound up in an interesting position -- I needed to exit out of a while loop from an inner switch-statement, something like this (pseudocode, obviously): :MyLoop While(foo) switch (bar) case '1': print '1'; break case '0': print '...

What is wrong with my do/while loop?

the following code gives me an error of: "expected ';' before '{' token". can anyone see why? do { r = rand() % numElements; } while ([questionsShown containsObject:r] && myCount < numElements) { //code here… } ...

PHP echo issue while in a while loop

I read in a csv file by using a while loop: while (($data = fgetcsv($handle, null, ",")) !== FALSE) and i want to skip the first row because this is the title row and i want to display on the screen "first line skipped". if($data[0]=="title") echo "Title row..skipping<br />"; else //do stuff The problem is since its in a while ...

How to do a while loop with a string redirected into it

Im trying to loop though a string with HTTP links inside and newlines, I want to loop over a line at a time. At the moment I have echo -e "$HTTP_LINKS" | while read HTTP_S_LINK ; do TEST_STRING="test" done But this way I don't have access to the TEST_STRING out side the loop, which is what I want. I'm using the while loop so that...

Coverting Letters to Numbers in a Telephone Number in C++

I have been staring at this all day and I am having a difficult time with this code. I need this program to convert a string of letters, such as Call Cash, into a 7 digit telephone number. I get either one of two outcomes. Either the program does not output any numbers at all or it gets stuck in an infinite loop. Any assistance would be ...

Perl, using variable from within While loop outside of the loop?

This seems really simple but it is giving me a hard time figuring it out as I'm new to perl.. I've been looking through a lot of documentation now about loops and I am still stumped by this... I have a sub that contains a where loop and I want to use a variable value from within the loop outside of the loop (after the loop has run), howe...

Cocoa: while(index >= 0) continuing, even though index == -1...

Once again :) I've got the following code: -(void)removeFilesWithPathIndices:(NSIndexSet*)indexSet { NSInteger index = [indexSet firstIndex]; while(index >= 0) { [self removeFileWithPathIndex:index]; index = [indexSet indexGreaterThanIndex:index]; } } Which should iterate through an NSIndexSet. However, th...

loop until all elements have been accessed N times in python

I have a group of buckets, each with a certain number of items in them. I want to make combinations with one item from each bucket. The loop should keep making different combinations until each item has participated in at least some defined number. I can easily see how to run the loop and stop once a single element has been accessed a c...

Else clause on Python while statement

Hi everyone, I've noticed the following code is legal in Python. My question is why? Is there a specific reason? n = 5 while n != 0: print n n -= 1 else: print "what the..." Thanks. ...

Do while loop in R

I have a Program in R where i'm using do while loop in the following format for(i in 1:n) { repeat { #code if(condition){break} } } I have a problem even if the condition in if statement is true the loop is been executed still.Can anyone help me with this Thank you ...

While loop doesn't work

$r = mysql_query ( "SELECT guid FROM characters" ); if (mysql_num_rows($r) != 0) { while ( $row == mysql_fetch_array ( $r ) ) { $cap = rand ( 0, $char ); if ($row ['guid'] != $cap) { $captain = rand ( 0, $char ); } } } else { $captain = rand ( 0, $char ); } This code should return me the...

c# loop comparisons

I use comboboxes a lot. I often loop through them to find a match based upon the SelectedValue, for example... while(cmbCompany.SelectedValue.ToString()!=B1ID) { cmbCompany.SelectedIndex++; } a) am I missing some much quicker option! b) if my comparison was against an integer, is there any benefit in declaring a string and setti...

do...while vs while

Possible Duplicates: While vs. Do While When should I use do-while instead of while loops? I've been programming for a while now (2 years work + 4.5 years degree + 1 year pre-college) and I've never used a do-while loop short of being forced to in the Introduction to Programming course. I have a growing feeling that I'm doin...

Images don't load while javascript while loop runs.

Hi, I'm writing an image pre-loader for my html5 project and I've struck a problem. Images that are used in the game are defined with the image object within JavaScript like so: images = new Array(); images[0] = new Image(); images[0].src = '/images/tiles/grass.png'; As soon as "document.ready" the pre-loader function as seen below i...

Bash, compare files. Removing values from file A that exist in File B

Greetings, I have one file - more or less a greylisting file. I need to compare the 40 to 50 values in it against a whitelisting file - and remove any values from the greylist that exists in the whitelist file. Right now I'm taking each greylist value and comparing it against each value in the whitelisting file (which has 1 - 2 thousa...

c while loop entry

hi somebody please explain me why int i=0,j=10; while(j>0,i++){ printf("%d%d",i,j); j--; } will not work and int i=0,j=10; while(i++,j>0){ printf("%d%d",i,j); j--; } works. Also please tell me why int i=0,j=10; while(j>0,++i){ printf("%d%d",i,j); j--; } gives an infinite loop ? thanks and regards ...