Forum Moderators: coopster

Message Too Old, No Replies

While( ) statment exceptions?

do only first time threw?

         

Sarah Atkinson

4:39 pm on May 20, 2005 (gmt 0)

10+ Year Member



Ihave a while statement and only the first time it cycles I want it to do something. All other times I simply want it to skip it. I though aboult something like
$x=0;
while($n=9){
if ($x==0)
//do whatever;
$x=1;
//do all the rest;
}

but is their an easer way?

Sarah

jatar_k

4:52 pm on May 20, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



if it needs to loop and do something only when a certain value is true, then that is the way to do it

I dont get this though
$x=1;

Sarah Atkinson

5:09 pm on May 20, 2005 (gmt 0)

10+ Year Member



let me add the brakets in (i don't include them when i have only one lined ifs

$x=0; //sets a boolean style verriable

while($n=9){
if ($x==0){
//do whatever;
}
$x=1; //basicly says that the while statment has executed at least one time
//do all the rest;
}

could also do something like ++$x; and keep a tally of how many times the while statment has cycled.

but I want a statment to happen the first time and only the first time and other statments to happen as normal.

Sarah

frizhard

8:03 pm on May 20, 2005 (gmt 0)

10+ Year Member



You could take the first statement out of the while loop ... thus simplifying the code ...

// do whatever
while($condition){
//do all the rest;
}

won't that work?