• 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
NSUInteger in for-loop?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSUInteger in for-loop?


  • Subject: NSUInteger in for-loop?
  • From: Alex Reynolds <email@hidden>
  • Date: Sun, 14 Sep 2008 21:15:53 -0700

I'm wondering if I'm using unsigned integers (specifically NSUInteger) properly or not.

I was under the impression that unsigned integers run from 0 to MAX_INT, but when I use them in a "for" loop within these bounds, the loop does not seem to always obey these constraints.

For example:

for (NSUInteger counter = 5; counter >= 0; --counter)
{
	NSLog(@"NSUInteger: %d", counter);
}

keeps running well after the "counter" variable turns negative:

2008-09-14 21:03:12.056 NSUIntTest[19579:10b] NSUInteger: 5
2008-09-14 21:03:12.059 NSUIntTest[19579:10b] NSUInteger: 4
2008-09-14 21:03:12.063 NSUIntTest[19579:10b] NSUInteger: 3
2008-09-14 21:03:12.068 NSUIntTest[19579:10b] NSUInteger: 2
2008-09-14 21:03:12.072 NSUIntTest[19579:10b] NSUInteger: 1
2008-09-14 21:03:12.077 NSUIntTest[19579:10b] NSUInteger: 0
2008-09-14 21:03:12.081 NSUIntTest[19579:10b] NSUInteger: -1
2008-09-14 21:03:12.085 NSUIntTest[19579:10b] NSUInteger: -2
...

However, the following does *not* run, starting from a negative integer (as I would expect):

for (NSUInteger counter = -5; counter <= 0; ++counter)
{
	NSLog(@"NSUInteger: %d", counter);
}

Also, the following works properly:

for (NSUInteger counter = 0; counter <= 5; ++counter)
{
	NSLog(@"NSUInteger: %d", counter);
}

I noticed that "uint" works the same way. Is this expected behavior?

Thanks,
Alex
_______________________________________________

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: NSUInteger in for-loop?
      • From: Brett Powley <email@hidden>
    • Re: NSUInteger in for-loop?
      • From: j o a r <email@hidden>
    • Re: NSUInteger in for-loop?
      • From: Nathan Kinsinger <email@hidden>
  • Prev by Date: Re: mailbundle info
  • Next by Date: Re: NSUInteger in for-loop?
  • Previous by thread: Re: brain-dead NSThread question ...
  • Next by thread: Re: NSUInteger in for-loop?
  • Index(es):
    • Date
    • Thread