while into while [message #176184] |
Wed, 30 November 2011 14:36 |
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 |
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 |
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
|
|
|