« 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:

>> 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':
>
> Hmmm...an interesting point, but I'm not entirely sure what the
> semantic difference in your statement would be when compared to my
> original wording. Sounds like two different ways of saying the same
> thing.
>
> After a 'break' there will be no other 'case' executed in the same
> 'switch' block, so the current 'case' has ended - which by definition
> implies the 'switch' has ended also (since you cannot have code
> inside the 'switch' that is not attached to a 'case', default or
> otherwise).  Am I missing something here...?  Something subtle
> perhaps ?

A break statement breaks out of the switch statement, and therefore also
breaks out of the sequence of statements between the last and the next
case -- but the latter is not all it does, since just breaking out of
the sequence of statements between the last and the next case doesn't
necessarily imply to also break out of the switch statement (which is
what it does).

To break out /only/ of the sequence of statements between the last and
the next case you could use a 'goto case N' statement (if you jump to
the next case).

  switch( variable ) {
    case CONSTANT1: // start of CONSTANT1 case
      if( condition1 )
        break; // breaks out of switch, not just of current case
      else if( condition2 )
        goto case CONSTANT2; // breaks out of current case, to next case

      // ... some code related to case CONSTANT1

      if( condition3 )
        break; // breaks out of switch

      // "Fall-through" from case CONSTANT1 to case CONSTANT2 if
      // condition3 is false.

      // end of 'case CONSTANT1' sequence of statements (as defined
      // by start of next 'case' sequence of statements)
    case CONSTANT2: // <<< 'goto case CONSTANT2' jumps here
      // ...
      break;
    //...
  }
  // <<< 'break' inside the switch jumps here


Note that there is not really a "case block" or "case statement". A
'case' keyword defines a label within the sequence of statements inside
a 'switch' statement, not a block. The meaning of the 'break' statement
is defined as "A break statement terminates execution of the smallest
enclosing switch or iteration statement."

This is probably what you meant, but I think "breaking out of the
current case" is something different than "breaking out of the switch",
even though the latter implies the former (because the former doesn't
imply the latter).

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'