views:

57

answers:

5

by this question what i mean is that if, by example, someone's username is "bob" then the while loop condition will be ($i < 10), and if the username is something else then the while loop condition will be ($i > 10)

if($username == "bob")
{
   //make this while loop condition: ($i < 10)
   // it means: while($i <10){ so stuff}
}
else
{
   //make the while loop condition: ($i >10)
}
A: 

Try making two different while loops within the if and else blocks.

Oren
A: 

Is this what you mean? You can just take a different path depending on the success or failure of the if. If the code inside is the same, just call a method to avoid duplicating it.

if($username == "bob")
{
   //make this while loop condition: ($i < 10)
   // it means: while($i <10){ so stuff}
   while ($i < 10) {
       call_method();
   }
}
else
{
   while ($i > 10) {
       call_method();
   }
}
WhirlWind
+4  A: 

Make do_stuff a function, then this is perfectly readable (although special-casing 'bob' seems doubtable at best).

if($username == "bob")
{
   while($i<10) {
       do_stuff();
   }
}
else
{
   while($i>10) {
       do_stuff();
   }
}
ChristopheD
and what if $username == bob then i remove the while loop but leave the code inside. and if its else than bob, i put the while loop...
linkcool
A: 

To keep your code simple:

if($username == "bob") { while ($i<10){ }

} else { while ($i>10){ }

}

There are other better ways to handle it but they may make your code difficult to understand like using eval which I dont like.

jini
+2  A: 
while( ($username == 'bob' && $i <10 ) XOR $i > 10){}

$username == 'bob' will be evaluated first if it comes out to be true then $i < 10 is evaluated.

$var1 XOR $var2 is true only when one of $var1, $var2 is true but not both.

But I myself will go with this.

TheMachineCharmer