« Return to Thread: using BREAK in 'C'

Re: using BREAK in 'C'

by Tamas Rudnai :: Rate this Message:

Reply to Author | View in Thread

Yes, that's correct, only need to keep in mind that break works only for the
inner-most loop or case statement. In Perl for example you can add a name to
the loop and switches so then you can break out of nested loops easy which
is completely missing from C -- if you want to do anything like that then
you need to use goto instead (which is stated as evil by many programmers
and they do burn those who use goto :-)).

Tamas


On Fri, Jun 26, 2009 at 8:17 AM, cdb <colin@...> 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?
>
> Code below (note formatting may get mangled).
>
> void I2C_hwStop()
> {
>     BYTE timeout = 50;
>
>     SSPCON2 = SSPCON2 | (1<<PEN); // set stoP condition
>
>     //In case the slave device gets stuck, we need to have a way of
> exiting.
>     //otherwise we are stuck here until the plug is pulled!
>     while (!(PIR1 & (1<<SSPIF)))
>     {
>           timeout --;
>           if (timeout == 0)
>            {
>              break;
>            }
>     }
>
>     PIR1 = PIR1 &~(1<<SSPIF);  //clear completed flag
> }
> //end I2C_hwStop
>
> Thanks
>
> Colin
> --
> cdb,  on 26/06/2009
>
>
>
>
>
>
> --
> http://www.piclist.com PIC/SX FAQ & list archive
> View/change your membership options at
> http://mailman.mit.edu/mailman/listinfo/piclist
>



--
http://www.mcuhobby.com
--
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'