• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Variable declaration in "for" loop initialization expression?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Variable declaration in "for" loop initialization expression?


  • Subject: Re: Variable declaration in "for" loop initialization expression?
  • From: Graham Cox <email@hidden>
  • Date: Thu, 18 Nov 2010 22:47:38 +1100

On 18/11/2010, at 10:28 PM, Oleg Krupnov wrote:

> Hi,
>
> I know, this is a very basic question, but after learning so many
> programming languages, my head seems like a mess.
>
> In C that is in Objective-C, what is the correct/preferable way to
> declare the counter variable in a for loop?
>
> Type i;
> for (i = 0; i < N; ++ i) { statements }
>
> or
>
> for (Type i = 0; i < N; ++i) { statements }


Both are correct, but the latter form is only legal in C99, so if you do not have C99 enabled, you'll get a syntax error. I don't think there is any significant advantage to either form, except the latter is perhaps a bit more compact in source code. It shouldn't make any difference to the object code.

> But what about for (;;) ? The compiler does not point to it as an
> error if I declare the counter inside the for () brackets, but I want
> to make sure!

Again, it's only legal in C99.

--Graham


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Variable declaration in "for" loop initialization expression?
      • From: Graham Cox <email@hidden>
References: 
 >Variable declaration in "for" loop initialization expression? (From: Oleg Krupnov <email@hidden>)

  • Prev by Date: Variable declaration in "for" loop initialization expression?
  • Next by Date: Re: Variable declaration in "for" loop initialization expression?
  • Previous by thread: Variable declaration in "for" loop initialization expression?
  • Next by thread: Re: Variable declaration in "for" loop initialization expression?
  • Index(es):
    • Date
    • Thread