Difference Between If Else Statement And Switch Statement in Tabular Form

Sno.Basic TermsIf Else StatementSwitch 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.WorkingIf Condition is true display some message otherwise display other messageSwitch Statement is similar to a series of If Statements on the same expression
3.Programming LanguageC,Cpp,Java,php etcC,Cpp,Java ,php
4.Data Type SupportInteger, Character, Pointer or Floating-point type or Boolean typeonly character or a integer datatype
5.NestingPopular for nesting of a loopNot as popular for nesting of a loop
6.UnderstandingIf more conditions are used, it is more difficult to understandEven if the number of conditions increase, switch statement is still easier to understand
7.Flow Chart

If Else Flow Chart Diagram
If Else Flow Chart Diagram

Switch Flow Chart Diagram
Switch Flow Chart Diagram
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)
Share with Friends :
Written by:

Leave a Reply

Your email address will not be published. Required fields are marked *