Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between IF-ELSE and SWITCH?

People also ask

What is better if-else or switch?

if-else better for boolean values: If-else conditional branches are great for variable conditions that result into a boolean, whereas switch statements are great for fixed data values. Speed: A switch statement might prove to be faster than ifs provided number of cases are good.

What is difference between if and if-else statement?

With the if statement, a program will execute the true code block or do nothing. With the if/else statement, the program will execute either the true code block or the false code block so something is always executed with an if/else statement.


They are pretty similar but each has a few special features.

switch

  • switch is usually more compact than lots of nested if else and therefore, more readable
  • If you omit the break between two switch cases, you can fall through to the next case in many C-like languages. With if else you'd need a goto (which is not very nice to your readers ... if the language supports goto at all).
  • In most languages, switch only accepts primitive types as key and constants as cases. This means it can be optimized by the compiler using a jump table which is very fast.
  • It is not really clear how to format switch correctly. Semantically, the cases are jump targets (like labels for goto) which should be flush left. Things get worse when you have curly braces:

    case XXX: {
    } break;
    

    Or should the braces go into lines of their own? Should the closing brace go behind the break? How unreadable would that be? etc.

  • In many languages, switch only accepts only some data types.

if-else

  • if allows complex expressions in the condition while switch wants a constant
  • You can't accidentally forget the break between ifs but you can forget the else (especially during cut'n'paste)
  • it accepts all data types.

The main difference is that switch despatches immediately to the case concerned, typically via an indexed jump, rather than having to evaluate all the conditions that would be required in an if-else chain, which means that code at the end of the chain is reached more slowly than code at the beginning.

That in turn imposes some restrictions on the switch statement that the if-else chain doesn't have: it can't handle all datatypes, and all the case values have to be constant.


IF else - IT is used for taking a decisions

Switch statement - It is used to test the value of the given variable against a list of case value .


Differences Between if-else and switch

  1. Expression inside if statement decide whether to execute the statements inside if block or under else block. On the other hand, expression inside switch statement decide which case to execute.

  2. If-esle statement checks for equality as well as for logical expression . On the other hand, switch checks only for equality.

  3. The if statement evaluates integer, character, pointer or floating-point type or boolean type. On the other hand, switch statement evaluates only character or a integer datatype.

  4. Sequence of execution is like either statement under if block will execute or statements under else block statement will execute. On the other hand the expression in switch statement decide which case to execute and if you do not apply a break statement after each case it will execute till the end of switch statement.

  5. If expression inside if turn outs to be false, statement inside else block will be executed. If expression inside switch statement turn out to be false then default statements is executed.

  6. It is difficult to edit if-else statements as it is tedious to trace where the correction is required. On the other hand it is easy to edit switch statements as they are easy to trace.

in one word we can say switch acts a little bit faster than if else statement!!!