« Return to Thread: using BREAK in 'C'

Re: using BREAK in 'C'

by Peter Restall :: Rate this Message:

Reply to Author | View in Thread


On Fri, 26 Jun 2009 17:17:54 +1000, cdb wrote:

> I'm writing some I2C code for someone using the hardware MSSP on a
> 18F242.
>
> The following code works, but I'm not sure that it should.
>
> 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'.  It doesn't matter if the break is is inside
another code block (ie. '{ break; }').  It will also only break out of one
loop construct (the inner-most); some non-C languages allow you to specify
the number of nestings to break out of, which is sometimes useful.

Your code will exit the 'while' loop once 'timeout' equals zero.

Regards,

Pete Restall
--
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'