More C programming questions ...

Roger Oberholtzer roger
Tue Feb 15 11:33:04 PST 2005


On Tue, 2005-02-15 at 16:53, Ben Duncan wrote:
> Can a "break" be used to bust out of a for loop:
> 
> example:
> 
> int idx = 0 ;
> int DONEGOOD = 0 ;
> int numberitems = 64 ;   /* Limit items we can have to 64 */
> char string[256] ;       /* MAKE big enuff so we do not overflow
>                              mydata in the file will always be 32  */
> 
> for (idx = 0 ; idx < numberitems ; idx++ )
> {
>      gogetmydata( string ) ;
>      if ( strncmp( string, "ITEMID", 6) == 0 )
>         {
>           DONEGOOD = 1 ;
>           break ;
>          }
> }

This is what a break is for. It leaves the enclosing while/do/for loop.
And it is a good way to do it. The 'bad' way is to use a goto to get
out.

+????????????????????????????+???????????????????????????????+
? Roger Oberholtzer          ?   E-mail: roger at opq.se        ?
? OPQ Systems AB             ?      WWW: http://www.opq.se/  ?
? Nybrogatan 66 nb           ?    Phone: Int + 46 8   314223 ?
? 114 41 Stockholm           ?   Mobile: Int + 46 733 621657 ?
? Sweden                     ?      Fax: Int + 46 8   314223 ?
+????????????????????????????+???????????????????????????????+



More information about the Linux-users mailing list