Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the priority with nested case, for and if loop?

I have a program similar to this:

switch(x)
{ case 1:

for(i=0; i<10; i++)
{
   if ((i==3) || (i==4) || (i==5) || (i==6) || (i==7))
   {
      if (foobar[i])
         break;    // i am talking about this break
   }
}
...further program
break;   /not this break

if foobar[i] is true, would the program break out of the case label or the for loop?

like image 438
suraj Avatar asked Nov 30 '22 03:11

suraj


2 Answers

The break follows a LIFO type nature, that is, the last break will come out of the first control structure. So, the break that you chose would break out of the for-loop, not the case.

like image 101
Binayaka Chakraborty Avatar answered Dec 05 '22 07:12

Binayaka Chakraborty


The for loop.

Please see: break Statement (C++):

The break statement ends execution of the nearest enclosing loop or conditional statement in which it appears. Control passes to the statement that follows the ended statement, if any.

like image 29
ProgramFOX Avatar answered Dec 05 '22 06:12

ProgramFOX