Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > switch statement

Reply
Thread Tools

switch statement

 
 
bob@coolgroups.com
Guest
Posts: n/a
 
      12-16-2007
I was just wondering if the switch statement usually generates more

efficient code than an if/else. If not, then what is the main
benefit

of the switch statement?
 
Reply With Quote
 
 
 
 
Saeed Amrollahi
Guest
Posts: n/a
 
      12-16-2007
On Dec 16, 7:28 am, (E-Mail Removed) wrote:
> I was just wondering if the switch statement usually generates more
>
> efficient code than an if/else. If not, then what is the main
> benefit
>
> of the switch statement?


As Stroustrup has written in section 6.3.2 of 3rd the switch statement
is equivalent to if/else statement except that:
1. Sometimes it is more readable that equivalent if/else, because
testing a value against some constants are explicit.
2. It can also lead to a better code generation. I think it is
implementation dependent.

The C++ Standard Document doesn't mention switch statements are more
efficient than if/else statement.
I think you use both statements from programming styles point of view.

Regards,
S. Amrollahi
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      12-16-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I was just wondering if the switch statement usually generates more
> efficient code than an if/else.


Measure!

> If not, then what is the main
> benefit of the switch statement?


Legibility.

--
Ian Collins.
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      12-16-2007
(E-Mail Removed) wrote:

> I was just wondering if the switch statement usually generates more
>
> efficient code than an if/else.


In some cases it does, in others it doesn't.

> If not, then what is the main benefit
>
> of the switch statement?


Better readability.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
My Switch statement get stuck on the last statement bthumber ASP .Net 5 01-29-2009 07:56 PM
if statement that, when false, skips first statement in its block, executes second? Jay McGavren Java 11 01-16-2006 05:49 PM
How do I do a conditional statement in a constant statement? tkvhdl@gmail.com VHDL 3 12-16-2005 06:13 PM
Which of switch statement and if-else statement takes less time to execute? swaroophr@gmail.com C Programming 21 08-02-2005 09:24 AM
exec "statement" VS. exec "statement" in globals(), locals() tedsuzman Python 2 07-21-2004 08:41 PM



Advertisments