switch statement

B

bob

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?
 
S

Saeed Amrollahi

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
 
I

Ian Collins

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.
 
R

Rolf Magnus

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top