Solved

Programming Help

Posted on 2007-11-14
4
247 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
maze travler 6 62
Quicksort a dynamic deque 33 73
PDF library for Delphi 2 128
FMX StringGrid1->Canvas->FillRect Problem 3 171
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

861 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