Solved

Programming Help

Posted on 2007-11-14
4
244 Views
Last Modified: 2010-05-18
I'm having trouble with c++ programming.  When would you prefer a switch over an if statement?
0
Comment
Question by:cooperk50
  • 2
4 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 50 total points
ID: 20282152
Basically : when you can use a switch, then do so :)

They're not really interchangeable. A switch is used when you have to check an expression for several values, like this :

        switch (expression) {
            case 1 : do_something_for_case_1(); break;
            case 5 : do_something_for_case_5(); break;
            default : do_something_default(); break;
        }

An if statement is used to check an expression to see whether it's true or false :

        if (expression) {
            do_something_true();
        }
        else {
            do_something_false();
        }
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20282166
Check here for more info :

        http://www.cplusplus.com/doc/tutorial/control.html

It's an interesting read.
0
 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 20292666
Use if-else statements in place of switch statements that have noncontiguous case expressions

If the case expressions are contiguous or nearly contiguous integer values, most compilers translate the switch statement as a jump table instead of a comparison chain. Jump tables generally improve performance because they reduce the number of branches to a single procedure call, and shrink the size of the control-flow code no matter how many cases there are. The amount of control-flow code that the processor must execute is also the same for all values of the switch expression.

However, if the case expressions are noncontiguous values, most compilers translate the switch statement as a comparison chain. Comparison chains are undesirable because they use dense sequences of conditional branches, which interfere with the processor's ability to successfully perform branch prediction. Also, the amount of control-flow code increases with the number of cases, and the amount of control-flow code that the processor must execute varies with the value of the switch expression.

For example, if the case expression are contiguous integers, a switch statement can provide good performance:

switch (grade)
{
   case 'A':
      ...
      break;
   case 'B':
      ...
      break;
   case 'C':
      ...
      break;
   case 'D':
      ...
      break;
   case 'F':
      ...
      break;

But if the case expression aren't contiguous, the compiler may likely translate the code into a comparison chain instead of a jump table, and that can be slow:

switch (a)
{
   case 8:
      // Sequence for a==8
      break;
   case 16:
      // Sequence for a==16
      break;
   ...
   default:
      // Default sequence
      break;
}

In cases like this, replace the switch with a series of if-else statements:

if (a==8) {
   // Sequence for a==8
}
else if (a==16) {
   // Sequence for a==16
}
...
else {
   // Default sequence
}

This explains it!

Well some we consider the readability as well!

Best Regards,
DeepuAbrahamK

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now