• 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: if statements vs. switch
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: if statements vs. switch


  • Subject: Re: if statements vs. switch
  • From: Ondra Cada <email@hidden>
  • Date: Wed, 4 Sep 2002 17:01:03 +0200

On Wednesday, September 4, 2002, at 04:00 , Jeremy Dronfield wrote:

A C question really, and a matter of interest rather than a problem. From the point of view of speed and/or efficiency, what are the relative merits of a chain of IF statements versus a switch?

Don't skip the code readability and maintainability. Matter of factly, with todays CPUs, it's generally *much* more important than speed/efficiency, unless you happen to be solving a bottleneck.

Luckily, both unequivocally point the same way: if possible, use switch.

Take the following as an example:

As ifs:
if ([prefs integerForKey:@"ControlsDisplaySide"] == 0) [myWindow setFrameTopLeftPoint:topLeft];
if ([prefs integerForKey:@"ControlsDisplaySide"] == 1) [myWindow setFrameTopLeftPoint:bottomLeft];
if ([prefs integerForKey:@"ControlsDisplaySide"] == 2) [myWindow setFrameTopLeftPoint:topRight];
if ([prefs integerForKey:@"ControlsDisplaySide"] == 3) [myWindow setFrameTopLeftPoint:bottomRight];

As switch:
switch ([prefs integerForKey:@"ControlsDisplaySide"]) {
case 0: [myWindow setFrameTopLeftPoint:topLeft]; break;
case 1: [myWindow setFrameTopLeftPoint:bottomLeft]; break;
case 2: [myWindow setFrameTopLeftPoint:topRight]; break;
case 3: [myWindow setFrameTopLeftPoint:bottomRight]; break;
}

Does the switch only read the "prefs" value once

Of course it does. Besides, it's ways and ways more intelligible.

Will one approach compile more efficiently (i.e. more compactly) than the other?

'Course it will. That's why it is limited to constants in 'case' labels (though I personally would prefer expressions allowed too, with a fallback to less efficient translation, but it is just not so).

Will changing the second thru' fourth "ifs" into "else ifs" actually make for cleaner running (I presume it would)?

'Course it would.

Note also that you should consider a conditional expression, too. With four possibilities it would not be the best solution, but with two (sometimes even three) of them I would prefer it myself:

[myWindow setFrameTopLeftPoint:[prefs integerForKey:@"ControlsDisplaySide"
]==0?top:bottom];
---
Ondra Cada
OCSoftware: email@hidden http://www.ocs.cz
private email@hidden http://www.ocs.cz/oc
_______________________________________________
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.

  • Follow-Ups:
    • Re: if statements vs. switch
      • From: Chris Ridd <email@hidden>
    • Re: if statements vs. switch
      • From: Jeremy Dronfield <email@hidden>
References: 
 >if statements vs. switch (From: Jeremy Dronfield <email@hidden>)

  • Prev by Date: Re: massive problems with new nibtool
  • Next by Date: Encoding an integer in another object
  • Previous by thread: Re: if statements vs. switch
  • Next by thread: Re: if statements vs. switch
  • Index(es):
    • Date
    • Thread