« Return to Thread: using BREAK in 'C'

Re: using BREAK in 'C'

by Gerhard Fiedler :: Rate this Message:

Reply to Author | View in Thread

Peter Restall wrote:

>> Break should only short circuit a loop, in error I've coded it into
>> an IF statement that is inside a while loop - so whilst Break works
>> for a while loop, should it work when in an IF statement inside a
>> loop?
>
> Evening Colin.
>
> The 'break' statement will break out of the inner-most enclosing 'for', or
> 'while', or the current 'case'.  

This is not quite right. A 'break' will break out of the enclosing
'switch' statement, not only the current 'case' of it. Using it at the
end of the 'case' to exit the 'switch' (not the current 'case') is
actually probably the most common use for 'break':

  switch( variable ) {
    case CONSTANT1:
      // ...
      break; // breaks out of switch, not of current case
    case CONSTANT2:
      // ...
      break; // breaks out of switch, not of current case
    //...
  }
  // <<< break inside the switch jumps here

Gerhard
--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist

 « Return to Thread: using BREAK in 'C'