On 7/30/07, Siddhesh Poyarekar siddhesh.poyarekar@gmail.com wrote:
- Which is more efficient, or is there no difference between them:
while(true) { ... if(condition) break; }
OR
while(condition) { ... }
Hmm...well, it depends. The first block will break after executing some instructions. It actually looks like a Do...While loop since it executes instructions FIRST and then CHECKs the condition.
The second block first checks the condition and then executes the instructions. So it really depends on the specific code. The general blocks can't be compared really as they are NOT the same.
- Does this code only look bad or does it execute slowly as well:
while(true) { if(condition1) { do_something; break; } if(condition2) { do_something; break; }
do_something;
break; }
Actually that piece of code will execute only ONCE. Why? Because you have a break just before the closing brace of the while loop.
If we ignore that then, again depends on the specific purpose of the code. Yes it does look bad and yes it will work slowly if the three conditions are rarely executed. It then makes sense to take them out and execute them separately but then thats not always possible.