• 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: 3 obj-c/cocoa beginner q's
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: 3 obj-c/cocoa beginner q's


  • Subject: Re: 3 obj-c/cocoa beginner q's
  • From: Lance Bland <email@hidden>
  • Date: Tue, 25 Mar 2003 09:28:26 -0500

On Tuesday, March 25, 2003, at 08:55 AM, Steve Bird wrote:

> On Tuesday, March 25, 2003, at 08:30 AM, Ben Dougall wrote:
>
>> _____________________________________________________
>> 1.
>> this is a bit of code from a book i'm following at the moment. it
>> feels like it might possibly be an inefficient way of doing things.
>> but then i could easily be wrong:
>>
>> for (i = 0; i < [myArray count]; i++) {
>> ....
>> ....
>> }
>>
>> doesn't this mean that the myArray object will get messaged for every
>> for loop execution? in other words if the array has 100 elements
>> myArray is going to get messaged a 100 times? or doesn't that matter?
>> wouldn't defining an int to hold [myArray count] before going into
>> the for loop, and using that int in the for loop arguments, therefore
>> only messaging myArray once, be a better way to do that? or not?
>
> --- A reasonable question, but the [myArray count] is messaged only
> once, because that's the way C for loops work. Read the details on
> FOR loops - the arguments are evaluated only once to determine
> parameters, then the loop is executed.
> For that reason it's bad practice to modify the termination
> condition ([myArray count] in this case), or the increment condition
> (i++), from within the loop.

This code:

- (unsigned)ack
{
printf("in ack\n");

return 5;
}

{
unsigned index;

for(index = 0; index < [self ack]; index++)
{
printf("in loop index:%u\n", index);
}

}

gives this output:

in ack
in loop index:0
in ack
in loop index:1
in ack
in loop index:2
in ack
in loop index:3
in ack
in loop index:4
in ack

-lance
------------------------------------
Lance Bland
mailto:email@hidden
VVI Is The #1 Visual-Report Tool Developer For Mac OS X
http://www.vvi.com
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Re: 3 obj-c/cocoa beginner q's (From: Steve Bird <email@hidden>)

  • Prev by Date: Re: determining format of NSImageRep
  • Next by Date: Arrays & For Loops
  • Previous by thread: Re: 3 obj-c/cocoa beginner q's
  • Next by thread: Re: 3 obj-c/cocoa beginner q's
  • Index(es):
    • Date
    • Thread