FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » while into while
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
while into while [message #176184] Wed, 30 November 2011 14:36 Go to next message
monsieur madame is currently offline  monsieur madame
Messages: 1
Registered: November 2011
Karma: 0
Junior Member
Hi,

I need some help with multiple while loops.

I need to set a variable about the count of loops I want.

For example, $i = 3; would do :

while($x[$i-2] < 1) {
while($x[$i-1] < 1) {
while($x[$i] < 1) {
...
}
}
}


and $i = 2 would do :

while($x[$i-1] < 1) {
while($x[$i] < 1) {
...
}
}



I can't find how to do it. I'm pretty sure I miss a small magic
trick....

If someone have an idea... Thanks
Re: while into while [message #176185 is a reply to message #176184] Wed, 30 November 2011 14:44 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
monsieur madame wrote:
> Hi,
>
> I need some help with multiple while loops.
>
> I need to set a variable about the count of loops I want.
>
> For example, $i = 3; would do :
>
> while($x[$i-2] < 1) {
> while($x[$i-1] < 1) {
> while($x[$i] < 1) {
> ...
> }
> }
> }
>
>
> and $i = 2 would do :
>
> while($x[$i-1] < 1) {
> while($x[$i] < 1) {
> ...
> }
> }
>
>
>
> I can't find how to do it. I'm pretty sure I miss a small magic
> trick....
>
> If someone have an idea... Thanks


I have an idea that's probably beyond most procedural languages..as a
straight coding exercise BUT you MAY be able to do it as a recursive call

Consider the following

function loopy($i)
{
while ($i && (something else))
{
// do your loopy stuff.
}
$i--;
if($i>0)
$i=loopy($i);
return $i;
}


That will exercise the same loop $i times.
Re: while into while [message #176192 is a reply to message #176184] Wed, 30 November 2011 16:01 Go to previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Wed, 30 Nov 2011 06:36:00 -0800, monsieur madame wrote:


> I need to set a variable about the count of loops I want.
>
> For example, $i = 3; would do :
>
> while($x[$i-2] < 1) {
> while($x[$i-1] < 1) {
> while($x[$i] < 1) {
> ...
> }
> }
> }
>
>
> and $i = 2 would do :
>
> while($x[$i-1] < 1) {
> while($x[$i] < 1) {
> ...
> }
> }

>
> I can't find how to do it. I'm pretty sure I miss a small magic
> trick....
>
> If someone have an idea... Thanks

Recursive function, something like (untested):

function recurseWhile ($x, $i)
{
while($x[$i-($i - 1)] < 1)
{
if ($i != 0)
{
recurseWhile ($x, $i - 1)
}
else
{
...
}
}
}

Rgds

Denis McMahon
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Browser doesn't support
Next Topic: PHP-HTML-Javascript opportunities with Yahoo R&D!!!
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri Sep 20 14:37:44 GMT 2024

Total time taken to generate the page: 0.02544 seconds