while-loops

Is There a More Efficient Way to Write Nested While Loops?

The code below shows nested while loops, but it's not the very efficient. Suppose I wanted to extend the code to include 100 nested while loops. Is there a better way to accomplish this task? <?php $count = 1; $num = 1; $options=3; while ( $num <=$options ) { echo "(".$num . ") "; $num1 = 1; $options1=3; whil...

PHP while(variable=mysql_fetch_assoc) - explanation

Hi, I have been working with C# so this is quite strange for me: while($variable=mysql_fetch_assoc) { $X=$variable['A']; $Y=$variable['B']; } If it is like an ordinary loop, then X,Y will be reset with every loop, right? I have not been able to look up in PHP manual how it works. I guess that in each loop it advances to ...

Error message when compiling while-loop expected identifier

When trying to compile an infinite while loop in xcode iphone view based application, it gives me an error that reads expected identifier or '(' before 'while'. I made it as simple as possible. Sorry about the picture. Code block was not working. If you want to see an image of the code, here is the link. http://www.freeimagehosting....

Can I have a block in begin/while statement in Ruby?

I'm trying out to have a block in a while and begin statements in Ruby, but I get a syntax error. Any other way to implement it? Here's what I want to accomplish (1..limit).each { |i| while (true) do |n| x = n * (i%n) puts n if n%i != 0 break if x.even? && !x.zero? n += 1 end } ...

What is exactly the off-by-one errors in the while loop?

What is exactly the off-by-one errors in the while loop?how do i figure it out and how to fix it? Thanks ...

What are the benefits of `while(condition) { //work }` and `do { //work } while(condition)`?

I found myself confronted with an interview question where the goal was to write a sorting algorithm that sorts an array of unsorted int values: int[] unsortedArray = { 9, 6, 3, 1, 5, 8, 4, 2, 7, 0 }; Now I googled and found out that there are so many sorting algorithms out there! Finally I could motivate myself to dig into Bubble So...

Using scanf in a while loop

Probably an extremely simple answer to this extremely simple question: I'm reading "C Primer Plus" by Pratta and he keeps using the example while (scanf("%d", &num) == 1)... Is the == 1 really necessary? It seems like one could just write: while (scanf("%d", &num)) It seems like the equality test is unnecessary since scanf returns...

VB.NET 2008 Application crashing during Do Loop

I am writing an application to compare each item on listbox1 to all items on listbox2. If the item is found, then remove it from both lists. The goal is to only have the items that were not found remain on both lists. The problem is, the application just hangs and I never get any results. I looked at my code several times and I cannot f...

Why do these nested while loops not work?

I tried and tried and tried to get this code to work and kept coming up with zilch. So I decided to try it using "for loops" instead and it worked first try. Could somebody tell me why this code is no good? <?php $x = $y = 10; while ($x < 100) { while ($y < 100) { $num = $x * $y; $numstr = strval($num); if (...

input of while loop to come from output of `command`

#I used to have this, but I don't want to write to the disk # pcap="somefile.pcap" tcpdump -n -r $pcap > all.txt while read line; do ARRAY[$c]="$line" c=$((c+1)) done < all.txt The following fails to work. # I would prefer something like... # pcap="somefile.pcap" while read line; do ARRAY[$c]="$line" c=$((c+1)) done ...

Why use a "do while" loop?

Hi, I've never understood why using a do while loops is necessary. I understand what they do, Which is to execute the code that the while loop contains without checking if the condition is true first. But isn't the below code: do{ document.write("ok"); } while(x == "10"); The exact same as: document.write("ok"); while(x == "10"...

Basic Python While loop compound conditional evaluation

In Python IDLE Shell it seems I cannot use a compound conditional expression and a while loop. I tried it within brackets too. Take these two examples: k=0 m=0 while k<10 & m<10: print k k +=1 m+=1 This doesn't evaluate the second condition. But if I write while k<10: print k k+=1 This does work. Is there a wa...

Is "}while(0);" always equal to "break;}while(1);" ?

I have compared gcc assembler output of do{ //some code }while(0); with do{ //some code break; }while(1); The output is equal, with or without optimization but.. It's always that way? No experiment can prove theories, they can only show they are wrong And because (I hope) programming is not an experimental science, and...

MySQL & PHP while code is out of memory, when there is only one row

Hey all, why is this code throwing an out of memory error, when there is only 1 row in the database.. $request_db = mysql_query("SELECT * FROM requests WHERE haveplayed='0'") or die(mysql_error()); $request = mysql_fetch_array( $request_db ); echo "<table border=\"1\" align=\"center\">";...

Get number of posts in a topic PHP

How do I get to display the number of posts on a topic like a forum. I used this... (how very noobish): function numberofposts($n) { $sql = "SELECT * FROM posts WHERE topic_id = '" . $n . "'"; $result = mysql_query($sql) or die(mysql_error()); $count = mysql_num_rows($result); echo numb...

Scala Unit type

I use opencsv to parse csv files, and my code is while( (line = reader.readNext()) != null ) { .... } I got a compiler warning saying: comparing values of types Unit and Null using `!=' will always yield true [warn] while( (aLine = reader.readNext()) != null ) { How should I do the while loop? ...

php , SimpleXML, while loop

I'm trying to get some information from ebay api and store it in database . I used simple xml to extract the information but I have a small issue as the information is not displayed for some items . if I make a print to the simple_xml I can see very well that the information is provided by ebay api . I have $items = "220617293997,25064...

Recursion inside while loop, How does it work ?

Can you please tell me how does this java code work? : public class Main { public static void main (String[] args) { Strangemethod(5); } public static void Strangemethod(int len) { while(len > 1){ System.out.println(len-1); Strangemethod(len - 1); } } } I tried to debug it ...

Call the Python interactive interpreter from within a Python script

Is there any way to start up the Python interpreter from within a script , in a manner similar to just using python -i so that the objects/namespace, etc. from the current script are retained? The reason for not using python -i is that the script initializes a connection to an XML-RPC server, and I need to be able to stop the entire prog...

My While Loop is not grabbing the MySQL database entries

Hi all, why isn't my while loop getting the mysql database entries and presenting them like demonstrated? Thanks :). <?php $djs_all_db = mysql_query("SELECT * FROM djs") or die(mysql_error()); $djs_all_num = mysql_num_rows($djs_all_db); while($djs_all = mysql_fetch_array( $djs_all_db )) { if ($djs_a...