SlideShare una empresa de Scribd logo
1 de 3
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”
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");
}
}
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";
}
}

Más contenido relacionado

La actualidad más candente

Machine language
Machine languageMachine language
Machine languageRipal Dhruv
 
Programming fundamentals lecture 1&2
Programming fundamentals lecture 1&2Programming fundamentals lecture 1&2
Programming fundamentals lecture 1&2Raja Hamid
 
Control Unit Design
Control Unit DesignControl Unit Design
Control Unit DesignVinit Raut
 
Computer network transmission channel & topology
Computer network transmission channel & topologyComputer network transmission channel & topology
Computer network transmission channel & topologySweta Kumari Barnwal
 
Sistem Operasi: Arsitektur komputer, Pengantar Sistem Operasi dan Kernel
Sistem Operasi:   Arsitektur komputer, Pengantar Sistem Operasi dan KernelSistem Operasi:   Arsitektur komputer, Pengantar Sistem Operasi dan Kernel
Sistem Operasi: Arsitektur komputer, Pengantar Sistem Operasi dan KernelAndino Maseleno
 
Online Fitness Gym Documentation
Online Fitness Gym Documentation Online Fitness Gym Documentation
Online Fitness Gym Documentation Abhishek Patel
 
Multi core processors
Multi core processorsMulti core processors
Multi core processorsAdithya Bhat
 
Presentation bca 1 c
Presentation   bca 1 cPresentation   bca 1 c
Presentation bca 1 cgursharan914
 
1.0 Introduction to Hardware Computer Architecture
1.0 Introduction to Hardware Computer Architecture1.0 Introduction to Hardware Computer Architecture
1.0 Introduction to Hardware Computer ArchitectureAbdelrahman Hosny
 
Real Time Operating system (RTOS) - Embedded systems
Real Time Operating system (RTOS) - Embedded systemsReal Time Operating system (RTOS) - Embedded systems
Real Time Operating system (RTOS) - Embedded systemsHariharan Ganesan
 
SDD Translation
SDD  TranslationSDD  Translation
SDD Translationgavhays
 
Memory organization
Memory organizationMemory organization
Memory organizationAL- AMIN
 

La actualidad más candente (20)

Multitasking
MultitaskingMultitasking
Multitasking
 
Control unit
Control  unitControl  unit
Control unit
 
Cache memory
Cache memoryCache memory
Cache memory
 
Machine language
Machine languageMachine language
Machine language
 
Programming fundamentals lecture 1&2
Programming fundamentals lecture 1&2Programming fundamentals lecture 1&2
Programming fundamentals lecture 1&2
 
Chapter 8
Chapter 8Chapter 8
Chapter 8
 
Cache memory
Cache memoryCache memory
Cache memory
 
Control Unit Design
Control Unit DesignControl Unit Design
Control Unit Design
 
Computer network transmission channel & topology
Computer network transmission channel & topologyComputer network transmission channel & topology
Computer network transmission channel & topology
 
Sistem Operasi: Arsitektur komputer, Pengantar Sistem Operasi dan Kernel
Sistem Operasi:   Arsitektur komputer, Pengantar Sistem Operasi dan KernelSistem Operasi:   Arsitektur komputer, Pengantar Sistem Operasi dan Kernel
Sistem Operasi: Arsitektur komputer, Pengantar Sistem Operasi dan Kernel
 
Online Fitness Gym Documentation
Online Fitness Gym Documentation Online Fitness Gym Documentation
Online Fitness Gym Documentation
 
Computer Languages
Computer Languages Computer Languages
Computer Languages
 
Cache memory
Cache memoryCache memory
Cache memory
 
Windows ce
Windows ceWindows ce
Windows ce
 
Multi core processors
Multi core processorsMulti core processors
Multi core processors
 
Presentation bca 1 c
Presentation   bca 1 cPresentation   bca 1 c
Presentation bca 1 c
 
1.0 Introduction to Hardware Computer Architecture
1.0 Introduction to Hardware Computer Architecture1.0 Introduction to Hardware Computer Architecture
1.0 Introduction to Hardware Computer Architecture
 
Real Time Operating system (RTOS) - Embedded systems
Real Time Operating system (RTOS) - Embedded systemsReal Time Operating system (RTOS) - Embedded systems
Real Time Operating system (RTOS) - Embedded systems
 
SDD Translation
SDD  TranslationSDD  Translation
SDD Translation
 
Memory organization
Memory organizationMemory organization
Memory organization
 

Destacado

Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handlingpinkpreet_kaur
 
100 images for visual brainstorming
100 images for visual brainstorming100 images for visual brainstorming
100 images for visual brainstormingMarc Heleven
 
The impact of innovation on travel and tourism industries (World Travel Marke...
The impact of innovation on travel and tourism industries (World Travel Marke...The impact of innovation on travel and tourism industries (World Travel Marke...
The impact of innovation on travel and tourism industries (World Travel Marke...Brian Solis
 
Open Source Creativity
Open Source CreativityOpen Source Creativity
Open Source CreativitySara Cannon
 
Reuters: Pictures of the Year 2016 (Part 2)
Reuters: Pictures of the Year 2016 (Part 2)Reuters: Pictures of the Year 2016 (Part 2)
Reuters: Pictures of the Year 2016 (Part 2)maditabalnco
 
The Six Highest Performing B2B Blog Post Formats
The Six Highest Performing B2B Blog Post FormatsThe Six Highest Performing B2B Blog Post Formats
The Six Highest Performing B2B Blog Post FormatsBarry Feldman
 
The Outcome Economy
The Outcome EconomyThe Outcome Economy
The Outcome EconomyHelge Tennø
 

Destacado (8)

Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handling
 
100 images for visual brainstorming
100 images for visual brainstorming100 images for visual brainstorming
100 images for visual brainstorming
 
Succession “Losers”: What Happens to Executives Passed Over for the CEO Job?
Succession “Losers”: What Happens to Executives Passed Over for the CEO Job? Succession “Losers”: What Happens to Executives Passed Over for the CEO Job?
Succession “Losers”: What Happens to Executives Passed Over for the CEO Job?
 
The impact of innovation on travel and tourism industries (World Travel Marke...
The impact of innovation on travel and tourism industries (World Travel Marke...The impact of innovation on travel and tourism industries (World Travel Marke...
The impact of innovation on travel and tourism industries (World Travel Marke...
 
Open Source Creativity
Open Source CreativityOpen Source Creativity
Open Source Creativity
 
Reuters: Pictures of the Year 2016 (Part 2)
Reuters: Pictures of the Year 2016 (Part 2)Reuters: Pictures of the Year 2016 (Part 2)
Reuters: Pictures of the Year 2016 (Part 2)
 
The Six Highest Performing B2B Blog Post Formats
The Six Highest Performing B2B Blog Post FormatsThe Six Highest Performing B2B Blog Post Formats
The Six Highest Performing B2B Blog Post Formats
 
The Outcome Economy
The Outcome EconomyThe Outcome Economy
The Outcome Economy
 

Similar a Difference between switch and ladder if

Decision Making and Branching in C
Decision Making and Branching  in CDecision Making and Branching  in C
Decision Making and Branching in CRAJ KUMAR
 
Selection Statements in C Programming
Selection Statements in C ProgrammingSelection Statements in C Programming
Selection Statements in C ProgrammingKamal Acharya
 
Chapter 4(1)
Chapter 4(1)Chapter 4(1)
Chapter 4(1)TejaswiB4
 
2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.pptManojKhadilkar1
 
BSc. III Unit iii VB.NET
BSc. III Unit iii VB.NETBSc. III Unit iii VB.NET
BSc. III Unit iii VB.NETUjwala Junghare
 
Chapter 8 - Conditional Statement
Chapter 8 - Conditional StatementChapter 8 - Conditional Statement
Chapter 8 - Conditional StatementDeepak Singh
 
C statements
C statementsC statements
C statementsAhsann111
 
Matlab: Control Statements
Matlab: Control StatementsMatlab: Control Statements
Matlab: Control Statementsmatlab Content
 
Decision control structures
Decision control structuresDecision control structures
Decision control structuresRahul Bathri
 
If statements in c programming
If statements in c programmingIf statements in c programming
If statements in c programmingArchana Gopinath
 
Selection statements
Selection statementsSelection statements
Selection statementsHarsh Dabas
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C pptMANJUTRIPATHI7
 
Switch Statements.pptx
Switch Statements.pptxSwitch Statements.pptx
Switch Statements.pptxEnricoValdez3
 
C language control statements
C language  control statementsC language  control statements
C language control statementssuman Aggarwal
 
05 Conditional statements
05 Conditional statements05 Conditional statements
05 Conditional statementsmaznabili
 
Switch Case in C Programming
Switch Case in C ProgrammingSwitch Case in C Programming
Switch Case in C ProgrammingSonya Akter Rupa
 

Similar a Difference between switch and ladder if (20)

Decision Making and Branching in C
Decision Making and Branching  in CDecision Making and Branching  in C
Decision Making and Branching in C
 
Selection Statements in C Programming
Selection Statements in C ProgrammingSelection Statements in C Programming
Selection Statements in C Programming
 
Chapter 4(1)
Chapter 4(1)Chapter 4(1)
Chapter 4(1)
 
2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt
 
BSc. III Unit iii VB.NET
BSc. III Unit iii VB.NETBSc. III Unit iii VB.NET
BSc. III Unit iii VB.NET
 
Chapter 8 - Conditional Statement
Chapter 8 - Conditional StatementChapter 8 - Conditional Statement
Chapter 8 - Conditional Statement
 
C statements
C statementsC statements
C statements
 
conditional.pptx
conditional.pptxconditional.pptx
conditional.pptx
 
Control Statements in Matlab
Control Statements in  MatlabControl Statements in  Matlab
Control Statements in Matlab
 
Matlab: Control Statements
Matlab: Control StatementsMatlab: Control Statements
Matlab: Control Statements
 
Decision control structures
Decision control structuresDecision control structures
Decision control structures
 
If statements in c programming
If statements in c programmingIf statements in c programming
If statements in c programming
 
Selection statements
Selection statementsSelection statements
Selection statements
 
Controls & Loops in C
Controls & Loops in C Controls & Loops in C
Controls & Loops in C
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C ppt
 
Switch Statements.pptx
Switch Statements.pptxSwitch Statements.pptx
Switch Statements.pptx
 
C language control statements
C language  control statementsC language  control statements
C language control statements
 
05 Conditional statements
05 Conditional statements05 Conditional statements
05 Conditional statements
 
C++ STATEMENTS
C++ STATEMENTS C++ STATEMENTS
C++ STATEMENTS
 
Switch Case in C Programming
Switch Case in C ProgrammingSwitch Case in C Programming
Switch Case in C Programming
 

Último

Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 

Último (20)

TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 

Difference between switch and ladder if

  • 1. 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”
  • 2. 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"); } }
  • 3. 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"; } }