PureBytes Links
Trading Reference Links
|
Is a "break" statement useful ?
It depends on the problem.
a. There are problems with 1 [or 0] solutions.
In this case the break statement would save the total iteration time.
The code solution is simple and does not need a new "break"
statement :
Example: Find the solution of x-50=0 in the [1,1000] range and stop
looping after the solution.
n=0;
for(i=1;i<1000;i++)
{
n=n+1;//the loop counter
if(i-50==0)
i=1000;//the break
}
Title=WriteVal(n,1.0)+" iterations";
We shall see 50 iterations, not 999.
b. There are problems with more than one solutions.
In this case the use of a break would be useless [and dangerous].
Example: Find the solution of (x-50)*(x-100)*(x-200)=0 in the
[1,1000] range and stop looping after the solution.
Unfortunalely the
n=0; x=0;
for(i=1;i<1000;i++)
{
n=n+1;
if((i-50)*(i-100)*(i-200)==0)
{
x=i;
i=1000;
}
}
Title=WriteVal(n,1.0)+" iterations"+", x="+WriteVal(x,1.0);
will make 50 iterations again, will find the [1st] solution and we
shal never know the rest x=100 or x=200.
c. In real problems we do not know in advance if they accept one or
more than one solutions. The use of a "break" statement would be
tricky, we would sleep with the [great] idea that all problems are 1-
dimensional.
I am afraid they are not...
Dimitris Tsokakis
Send BUG REPORTS to bugs@xxxxxxxxxxxxx
Send SUGGESTIONS to suggest@xxxxxxxxxxxxx
-----------------------------------------
Post AmiQuote-related messages ONLY to: amiquote@xxxxxxxxxxxxxxx
(Web page: http://groups.yahoo.com/group/amiquote/messages/)
--------------------------------------------
Check group FAQ at: http://groups.yahoo.com/group/amibroker/files/groupfaq.html
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/amibroker/
<*> To unsubscribe from this group, send an email to:
amibroker-unsubscribe@xxxxxxxxxxxxxxx
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
|