5
Difference between Switch and ladder if:- If else :-- else if is the control statement which controls the statement(s) to be executed on the basis of some condition. When we use else- if, the condition is checked and if the condition is true corresponding statement(s) is executed. If the condition is false, it continues checking the next else-if statement until the condition becomes true or the control comes to the end of all else-ifs. switch :--In this case, a statement is written inside switch, and then, the probabale cases are written. Now, when control comes to the switch statement, it compares the value with the cases, if it matches any cases, corresponding statements inside the case is executed n the control checks for next statement, and do the same, until it reaches the end of the switch. To, make the prog execute only one case, v use break inside every case. Diff :---- In case of else-if, the control checks every else-if statement until it find true value of the statement or it comes to the end of the else-ifs. In case of switch, according to the value of switch, the control jumps to the corresponding case. When, we want multiple types of statements to be executed when condition becomes true, we should use switch without break in the case. Whereas in case of else-if, we will have to use the common statements in desired else-ifs, which causes duplication of some of the statements. Example:- If …. else ladder Switch Switch(a) { case 1 : cout<<”red”; break; case 2: cout<<”orange”; break; case 3: cout<<”yellow”; If(a==0) Cout<<”red”; Else if (a==1) Cout<<”orange”; Else if (a==2) Cout<<”yellow”

Difference between switch and ladder if

Embed Size (px)

DESCRIPTION

Difference between switch and ladder if in c language

Citation preview

Page 1: Difference between switch and ladder if

Difference between Switch and ladder if:-

If else :-- else if is the control statement which controls the statement(s) to be executed on the basis of some condition. When we use else-if, the condition is checked and if the condition is true corresponding statement(s) is executed. If the condition is false, it continues checking the next else-if statement until the condition becomes true or the control comes to the end of all else-ifs. 

switch :--In this case, a statement is written inside switch, and then, the probabale cases are written. Now, when control comes to the switch statement, it compares the value with the cases, if it matches any cases, corresponding statements inside the case is executed n the control checks for next statement, and do the same, until it reaches the end of the switch. To, make the prog execute only one case, v use break inside every case.

Diff :---- In case of else-if, the control checks every else-if statement until it find true value of the statement or it comes to the end of the else-ifs. In case of switch, according to the value of switch, the control jumps to the corresponding case. When, we want multiple types of statements to be executed when condition becomes true, we should use switch without break in the case. Whereas in case of else-if, we will have to use the common statements in desired else-ifs, which causes duplication of some of the statements.

Example:-

If …. else ladder Switch

Switch(a)

{

case 1 : cout<<”red”;

break;

case 2: cout<<”orange”;

break;

case 3: cout<<”yellow”;

break;

}

If(a==0)

Cout<<”red”;

Else if (a==1)

Cout<<”orange”;

Else if (a==2)

Cout<<”yellow”

Page 2: Difference between switch and ladder if

Check a Character is vowel or not

If..else switch#include<stdio.h>main(){

char n;printf("enter charcter");scnaf("%c",&n);if(n=='a'||n=='e'||n=='i'||n=='o'||n=='u'){

printf("vowels");}else{

printf("not vowels");}

}

#include<stdio.h>main(){

char n;printf("enter charcter");scanf("%c",&n);switch(n){

case 'a':case 'e':case 'i':case 'o':case 'u':printf("vowel");break;default:

printf("not vowel");}

}

Page 3: Difference between switch and ladder if

Week Day Program#include<iostream>using namespace std;main(){

int week;cout<<"enter day";cin>>week;if(week==0){

cout<<"sunday";}else if(week==1){

cout<<"monday";}else if(week==2){

cout<<"tusday";}else if(week==3){

cout<<"wensday";}else if(week==4){

cout<<"thusday";}else if(week==5){

cout<<"friday";}else if(week==6){

cout<<"satuday";}else{

cout<<"wrong input";}

}

#include<iostream>using namespace std;main(){

int week;cout<<"enter day";cin>>week;switch(week){

case 1:cout<<"monday";break;

case 2:cout<<"tuseday";break;

case 3:cout<<"wensday";break;

case 4:cout<<"thusday";break;

case 5:cout<<"friday";break;

case 6:cout<<"satuday";break;

case 7:cout<<"sunday";break;

default:cout<<"wrong input";

}}