Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

break; C++: which loop is it actually breaking

Tags:

c++

c

loops

break

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 **simplex is inserted at most once in double **bestList
  • The instance of break here breaks out of the second (inner) for loop.

Is this the case?

like image 260
kiriloff Avatar asked May 14 '12 16:05

kiriloff


People also ask

Does break in C only exit one loop?

Therefore, break applies to only the loop within which it is present. Infinite Loops: break statement can be included in an infinite loop with a condition in order to terminate the execution of the infinite loop.

Does Break Break every loop?

Using break in a nested loop In a nested loop, a break statement only stops the loop it is placed in. Therefore, if a break is placed in the inner loop, the outer loop still continues. However, if the break is placed in the outer loop, all of the looping stops.

How many loops does break break C?

The break statement is used inside loops or switch statement. The break statement breaks the loop one by one, i.e., in the case of nested loops, it breaks the inner loop first and then proceeds to outer loops.

How does break work in C?

When a break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop. It can be used to terminate a case in the switch statement (covered in the next chapter).


3 Answers

The break statement in C++ will break out of the for or switch statement in which the break is directly placed. In this case it will break out of the for (int j = ... loop.

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.

// Causes the next iteration of the 'for (int i ...' loop to end the loop)
i = npts;

// Ends the 'for (int j ...' loop
break;
like image 180
JaredPar Avatar answered Sep 28 '22 01:09

JaredPar


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:

    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; 
            }
        }
        // after the 'break' you will end up here
    }

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.

like image 44
Sergey K. Avatar answered Sep 28 '22 01:09

Sergey K.


You are breaking out of your second loop to your first loop.

for (int i=0; i<npts; i++)

You could set a boolean at the top

bool shouldBreak = false;

and when you write break, write

shouldBreak = true;
break;

Then at the end of your loop, check each time,

if (shouldBreak) break;
like image 44
Martol1ni Avatar answered Sep 28 '22 01:09

Martol1ni