More C programming questions ...

Rick Sivernell res005ru
Tue Feb 15 13:47:48 PST 2005


On Tue, 15 Feb 2005 17:43:43 +0100
Roger Oberholtzer <roger at opq.se> wrote:

> 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 ?
> +????????????????????????????+???????????????????????????????+
> 
> _______________________________________________
> Linux-users mailing list
> Linux-users at linux-sxs.org
> http://mail.linux-sxs.org/cgi-bin/mailman/listinfo/linux-users
> 
> Need to chat further on this subject? Check out #linux-users on
> irc.linux-sxs.org !

All

> > for (idx = 0 ; idx < numberitems ; idx++ )
 {
      gogetmydata( string ) ;
      if (! strncmp( string, "ITEMID", 6) ) // less work for the compiler & typing
      {
         DONEGOOD = 1 ;
         break ;
     }
 }

cheers
-- 
Rick Sivernell
Dallas, Texas  75287
972 306-2296
res005ru at verizon.net
Registered Linux User

   .~.
  / v \
 /( _ )\
   ^ ^
In Linux we trust!


More information about the Linux-users mailing list