1

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)
}
1
  • I think this becomes a design issue. Try using the strategy pattern (Google it) and you can have some methods on your interface direct the loop / condition. Commented Apr 25, 2010 at 22:20

5 Answers 5

4

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();
   }
}
Sign up to request clarification or add additional context in comments.

1 Comment

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...
2
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.

Comments

0

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

Comments

0

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();
   }
}

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.