• 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: Unnecessary Boolean Warning
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Unnecessary Boolean Warning


  • Subject: Re: Unnecessary Boolean Warning
  • From: Sander Stoks <email@hidden>
  • Date: Thu, 04 Aug 2011 09:55:58 +0200

>> One important difference for instance is that if you write if (a() &
>> b()), both a() and b() will always be executed, while if you write if
>> (a() && b()), b() will be executed only if a() is true.
>
>
> The C language doesn't make any guarantees about that. While this
> optimisation is to be expected, the order of execution (left to right)
> and the optimisation (b not executed) is implementation dependent.
>
> This is a classic question for coding job interviews.

As has been pointed out, it most certainly does.  This is important
because it allows you to write stuff like

if (index < maxIndex && isValid(array[index]))
   ...

In languages which don't guarantee short-circuiting, the array could be
indexed out-of-bounds.

On a related note, somebody said he would be less confused if C didn't
have "two different kind of booleans".  In fact, it has none at all.  It
is part of the idiom that you should read "if(a)" as "if a is non-zero"
and not as "if a is true".

Regards,
Sander


_______________________________________________

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: Unnecessary Boolean Warning
      • From: Boyd Collier <email@hidden>
  • Prev by Date: Re: Unnecessary Boolean Warning
  • Next by Date: Re: How to Identify if the device is used in iAD supported Regions
  • Previous by thread: Re: Unnecessary Boolean Warning
  • Next by thread: Re: Unnecessary Boolean Warning
  • Index(es):
    • Date
    • Thread