simple question regarding C++ code:
for(int i=0;i<npts;i++)
{
for(int j=i;j<2*ndim;j++)
{
if(funcEvals[i]<bestListEval[j])
{
bestListEval[j] = funcEvals[i];
for(int k=0;k<m_ndim;k++)
bestList[j][k] = simplex[i][k];
break;
}
}
}
I want to ensure that
- Each line of
double **simplexis inserted at most once indouble **bestList - The instance of
breakhere breaks out of the second (inner)forloop.
Is this the case?
The break statement in C++ will break out of the for or switch statement in which the break is directly placed. It breaks the innermost structure (loop or switch). In this case:
There is no way in C++ to have break target any other loop. In order to break out of parent loops you need to use some other independent mechanism like triggering the end condition.
Also, if you want to exit more than one inner-loop you can extract that loops into a function. In C++ 11 lambdas can be used to do it in-place – so there will be no need to use goto.