Difference Between If Else Statement And Switch Statement in Tabular Form
Sno. | Basic Terms | If Else Statement | Switch Statement |
1. | Syntax | //If Else Syntax if (variable1 > Variable2) { operation } else { operation }
| //Switch Syntax switch (variable) { case 0: operation break; case 1: operation break; default: operation break; } |
2. | Working | If Condition is true display some message otherwise display other message | Switch Statement is similar to a series of If Statements on the same expression |
3. | Programming Language | C,Cpp,Java,php etc | C,Cpp,Java ,php |
4. | Data Type Support | Integer, Character, Pointer or Floating-point type or Boolean type | only character or a integer datatype |
5. | Nesting | Popular for nesting of a loop | Not as popular for nesting of a loop |
6. | Understanding | If more conditions are used, it is more difficult to understand | Even if the number of conditions increase, switch statement is still easier to understand |
7. | Flow Chart | ||
8. | Example | <?php $variable = 3; if($variable==0){ echo("Sunday"); }else if($variable==1){ echo("Monday"); }else if($variable==2){ echo("Tuesday"); }else if($variable==3){ echo("Wednesday"); }else if($variable==4){ echo("Thursday"); }else if($variable==5){ echo("Friday"); }else if($variable==6){ echo("Saturday"); }else{ echo("not Found Record"); } ?> | <?php $variable = 3; switch($variable){ case 0: echo("Sunday"); break; case 1: echo("Monday"); break; case 2: echo("Tuesday"); break; case 3: echo("Wednesday"); break; case 4: echo("Thursday"); break; case 5: echo("Friday"); break; case 6: echo("Saturday"); break; default: echo("not Found Record"); } ?> |
(Visited 1,168 times, 1 visits today)
Written by: