I'm not the best looper here on WT, so I have this question for those of you who are
Let's say I have an array that's taking randomly generated words and checking if they are the word given. So say we have any 4-letter combination of any character, and we are checking if it says 'asdf'. This could take forever, and it is definitely possible. There are 1500625 (?) different combinations to this (1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ) in a four-character sequence.
Basically what I'm trying to say (and failing) is how can I actively and efficiently run huge loops without it lagging or having to wait for the loop to complete before displaying anything?
We could say I wanted to run 5000000 iterations to check if x (which is a random string) is equal to "asdf". That'll take a bit. But what if the program finds a match on the 2723'rd iteration? We'd have to wait until it completes the entire loop before we find everything. I need to make the loop stop when that happens.
Thanks
-PG
__________________ Check out my Please login or register to view this content. Registration is FREE or my Please login or register to view this content. Registration is FREE !
Last edited by Physicsguy; 12-22-2010 at 08:02 PM..
__________________ Check out my Please login or register to view this content. Registration is FREE or my Please login or register to view this content. Registration is FREE !
__________________ Check out my Please login or register to view this content. Registration is FREE or my Please login or register to view this content. Registration is FREE !
Is using Break; just bad practice or does it cause functionality issues? If there's multiple nested loops, it would be easier using break; than having variables like $found, $found1, $found2.
To be fair there are different schools of thought regarding break.
I discourage using break purely from a design perspective. I would argue that instead of using variables like $found $found1 $found2.. etc you should use variables with meaningful names that will lend some insight into what is going on to another programmer.
In general I think interrupting the control flow of your program makes it easier to introduce bugs, especially when modifying your code. Here is a simple example:
PHP Code:
$i = 0; while($i < 10) { if($i == 5) continue;
$i++; }
This example is a little trivial, but in a real world implementation you might not realize that code similar to that above is actually an infinite loop.