SlideShare une entreprise Scribd logo
1  sur  17
Télécharger pour lire hors ligne
Switch – Case Statements
By: Er. Aman Kumar
Switch-Case Statements
way2ITech
• Switch case statement allows us to make
decisions from multiple choices.
• Integer expression must yield an integer value
like 1, 2, 3, etc. The keyword case is followed
by an integer or a character constant.
• Each constant in each case must be different
from all the others. The “do this” lines in the
above form of switch represent any valid C
statement.
way2ITech
way2ITech
• break always used with the switch statement
to terminate the switch statement or to
prevent the entry in other case.
way2ITech
way2ITech
way2ITech
The Tips and Traps
• It is not compulsory to put cases in ascending
order as we did above. We can place them in
any order we want.
way2ITech
You are also allowed to use char
values in case and switch as
shown in the following program
(Program to find whether character
entered is vowel or not)
way2ITech
way2ITech
Modified
Form
way2ITech
If a statement doesn’t belong to any case the compiler
won’t report an error. However, the statement would
never get executed. For example, in the following
program the printf( ) never goes to work.
way2ITech
• If there is no default in switch, then error will not be
there. If no case is matching, statements after switch
will execute.
• Relational Expression like i>5 are not allowed in any
case. Even float values not allowed in any case
• We can check the value of any expression in a switch.
Thus the following switch statements are legal.
switch ( i - j * k )
switch ( 3 + 9 % 4 * i )
switch ( a < 5 || b > 7 )
way2ITech
• Expressions can also be used in cases provided they
are constant expressions. Thus case 13 + 7 is correct,
however, case a + b is incorrect.
• switch may occur within another, but in practice it is
rarely done. Such statements would be called nested
switch statements.
• The switch statement is very useful while writing
menu driven programs
• Even if there are multiple statements to be executed
in each case there is no need to enclose them
within a pair of braces (unlike if, and else).
way2ITech
switch Versus if-else Ladder
Disadvantages of switch over if-else
• A float expression cannot be tested using a
switch
• Cases can never have variable expressions
(for example it is wrong to say case a +3 : )
• Multiple cases cannot use same expressions.
Thus the following switch is illegal:
way2ITech
Advantages of switch over if-else
• compiler generates a jump table for a switch
during compilation. As a result, during
execution it simply refers the jump table to
decide which case should be executed, rather
than actually checking which case is satisfied.
• As against this, if-else are slower because they
are evaluated at execution time.
way2ITech
10. switch case

Contenu connexe

Tendances

If else statement in c++
If else statement in c++If else statement in c++
If else statement in c++Bishal Sharma
 
Type casting in c programming
Type casting in c programmingType casting in c programming
Type casting in c programmingRumman Ansari
 
While , For , Do-While Loop
While , For , Do-While LoopWhile , For , Do-While Loop
While , For , Do-While LoopAbhishek Choksi
 
Variables in C Programming
Variables in C ProgrammingVariables in C Programming
Variables in C Programmingprogramming9
 
Decision making statements in C programming
Decision making statements in C programmingDecision making statements in C programming
Decision making statements in C programmingRabin BK
 
Control structures in java
Control structures in javaControl structures in java
Control structures in javaVINOTH R
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statementsSaad Sheikh
 
Nesting of if else statement & Else If Ladder
Nesting of if else statement & Else If Ladder Nesting of if else statement & Else If Ladder
Nesting of if else statement & Else If Ladder Vishvesh Jasani
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control StructureSokngim Sa
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C LanguageShaina Arora
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in JavaNiloy Saha
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C ProgrammingShuvongkor Barman
 
Nested loop in C language
Nested loop in C languageNested loop in C language
Nested loop in C languageErumShammim
 

Tendances (20)

If else statement in c++
If else statement in c++If else statement in c++
If else statement in c++
 
Type casting in c programming
Type casting in c programmingType casting in c programming
Type casting in c programming
 
While , For , Do-While Loop
While , For , Do-While LoopWhile , For , Do-While Loop
While , For , Do-While Loop
 
Variables in C Programming
Variables in C ProgrammingVariables in C Programming
Variables in C Programming
 
Decision making statements in C programming
Decision making statements in C programmingDecision making statements in C programming
Decision making statements in C programming
 
Control structures in java
Control structures in javaControl structures in java
Control structures in java
 
String functions in C
String functions in CString functions in C
String functions in C
 
Data types in java
Data types in javaData types in java
Data types in java
 
Strings
StringsStrings
Strings
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
C if else
C if elseC if else
C if else
 
Loops c++
Loops c++Loops c++
Loops c++
 
Nesting of if else statement & Else If Ladder
Nesting of if else statement & Else If Ladder Nesting of if else statement & Else If Ladder
Nesting of if else statement & Else If Ladder
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in Java
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
 
Nested loop in C language
Nested loop in C languageNested loop in C language
Nested loop in C language
 

En vedette

Switch case and looping statement
Switch case and looping statementSwitch case and looping statement
Switch case and looping statement_jenica
 
Lecture13 control statementswitch.ppt
Lecture13 control statementswitch.pptLecture13 control statementswitch.ppt
Lecture13 control statementswitch.ppteShikshak
 
Looping and switch cases
Looping and switch casesLooping and switch cases
Looping and switch casesMeoRamos
 
Basics of C programming
Basics of C programmingBasics of C programming
Basics of C programmingavikdhupar
 
C language control statements
C language  control statementsC language  control statements
C language control statementssuman Aggarwal
 
Token system powerpoint
Token system powerpointToken system powerpoint
Token system powerpointMatthew McCall
 
Branching in PowerPoint
Branching in PowerPointBranching in PowerPoint
Branching in PowerPointKarl Kapp
 
Types of operators in C
Types of operators in CTypes of operators in C
Types of operators in CPrabhu Govind
 
Prsentation on functions
Prsentation on functionsPrsentation on functions
Prsentation on functionsAlisha Korpal
 
Chapter 05 looping
Chapter 05   loopingChapter 05   looping
Chapter 05 loopingDhani Ahmad
 
C Language - Switch and For Loop
C Language - Switch and For LoopC Language - Switch and For Loop
C Language - Switch and For LoopSukrit Gupta
 

En vedette (20)

Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and looping
 
Switch case and looping statement
Switch case and looping statementSwitch case and looping statement
Switch case and looping statement
 
Lecture13 control statementswitch.ppt
Lecture13 control statementswitch.pptLecture13 control statementswitch.ppt
Lecture13 control statementswitch.ppt
 
Looping and switch cases
Looping and switch casesLooping and switch cases
Looping and switch cases
 
C notes.pdf
C notes.pdfC notes.pdf
C notes.pdf
 
C ppt
C pptC ppt
C ppt
 
Basics of C programming
Basics of C programmingBasics of C programming
Basics of C programming
 
C language ppt
C language pptC language ppt
C language ppt
 
C language control statements
C language  control statementsC language  control statements
C language control statements
 
Tokens_C
Tokens_CTokens_C
Tokens_C
 
Token system powerpoint
Token system powerpointToken system powerpoint
Token system powerpoint
 
Branching in PowerPoint
Branching in PowerPointBranching in PowerPoint
Branching in PowerPoint
 
Types of operators in C
Types of operators in CTypes of operators in C
Types of operators in C
 
C tokens
C tokensC tokens
C tokens
 
Prsentation on functions
Prsentation on functionsPrsentation on functions
Prsentation on functions
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 
Decision making and looping
Decision making and loopingDecision making and looping
Decision making and looping
 
Chapter 05 looping
Chapter 05   loopingChapter 05   looping
Chapter 05 looping
 
C Language - Switch and For Loop
C Language - Switch and For LoopC Language - Switch and For Loop
C Language - Switch and For Loop
 
Functions in C
Functions in CFunctions in C
Functions in C
 

Similaire à 10. switch case

CIS 1403 lab 4 selection
CIS 1403 lab 4 selectionCIS 1403 lab 4 selection
CIS 1403 lab 4 selectionHamad Odhabi
 
Cprogrammingprogramcontrols
CprogrammingprogramcontrolsCprogrammingprogramcontrols
Cprogrammingprogramcontrolsteach4uin
 
Bt0067 c programming and data structures 1
Bt0067 c programming and data structures 1Bt0067 c programming and data structures 1
Bt0067 c programming and data structures 1Techglyphs
 
9. statements (conditional statements)
9. statements (conditional statements)9. statements (conditional statements)
9. statements (conditional statements)Way2itech
 
Control Structures.pptx
Control Structures.pptxControl Structures.pptx
Control Structures.pptxssuserfb3c3e
 
Control statements anil
Control statements anilControl statements anil
Control statements anilAnil Dutt
 
Learn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & LoopsLearn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & LoopsEng Teong Cheah
 
Switch Case in C Program
Switch Case in C ProgramSwitch Case in C Program
Switch Case in C ProgramRup Chowdhury
 
SWITCH-CASE, Lesson Computer Programming.pptx
SWITCH-CASE, Lesson Computer Programming.pptxSWITCH-CASE, Lesson Computer Programming.pptx
SWITCH-CASE, Lesson Computer Programming.pptxAlwinJamesPuracan
 
Control statements
Control statementsControl statements
Control statementsCutyChhaya
 
Chapter05-Control Structures.pptx
Chapter05-Control Structures.pptxChapter05-Control Structures.pptx
Chapter05-Control Structures.pptxAdrianVANTOPINA
 
C language UPTU Unit3 Slides
C language UPTU Unit3 SlidesC language UPTU Unit3 Slides
C language UPTU Unit3 SlidesRakesh Roshan
 
Cse lecture-6-c control statement
Cse lecture-6-c control statementCse lecture-6-c control statement
Cse lecture-6-c control statementFarshidKhan
 

Similaire à 10. switch case (20)

CIS 1403 lab 4 selection
CIS 1403 lab 4 selectionCIS 1403 lab 4 selection
CIS 1403 lab 4 selection
 
22 Jop Oct 08
22 Jop Oct 0822 Jop Oct 08
22 Jop Oct 08
 
Cprogrammingprogramcontrols
CprogrammingprogramcontrolsCprogrammingprogramcontrols
Cprogrammingprogramcontrols
 
Bt0067 c programming and data structures 1
Bt0067 c programming and data structures 1Bt0067 c programming and data structures 1
Bt0067 c programming and data structures 1
 
Final requirement
Final requirementFinal requirement
Final requirement
 
9. statements (conditional statements)
9. statements (conditional statements)9. statements (conditional statements)
9. statements (conditional statements)
 
Control Structures.pptx
Control Structures.pptxControl Structures.pptx
Control Structures.pptx
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
 
Learn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & LoopsLearn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & Loops
 
Switch Case in C Program
Switch Case in C ProgramSwitch Case in C Program
Switch Case in C Program
 
SWITCH-CASE, Lesson Computer Programming.pptx
SWITCH-CASE, Lesson Computer Programming.pptxSWITCH-CASE, Lesson Computer Programming.pptx
SWITCH-CASE, Lesson Computer Programming.pptx
 
Control statements
Control statementsControl statements
Control statements
 
Final requirement
Final requirementFinal requirement
Final requirement
 
C programming session3
C programming  session3C programming  session3
C programming session3
 
C programming session3
C programming  session3C programming  session3
C programming session3
 
Chapter05-Control Structures.pptx
Chapter05-Control Structures.pptxChapter05-Control Structures.pptx
Chapter05-Control Structures.pptx
 
C language UPTU Unit3 Slides
C language UPTU Unit3 SlidesC language UPTU Unit3 Slides
C language UPTU Unit3 Slides
 
Cse lecture-6-c control statement
Cse lecture-6-c control statementCse lecture-6-c control statement
Cse lecture-6-c control statement
 
Control structure
Control structureControl structure
Control structure
 

Plus de Way2itech

Escape sequences
Escape sequencesEscape sequences
Escape sequencesWay2itech
 
Variables in C and C++ Language
Variables in C and C++ LanguageVariables in C and C++ Language
Variables in C and C++ LanguageWay2itech
 
Compilation of c
Compilation of cCompilation of c
Compilation of cWay2itech
 
8. operators
8. operators8. operators
8. operatorsWay2itech
 
7. input and output functions
7. input and output functions7. input and output functions
7. input and output functionsWay2itech
 
Introduction to c_language
Introduction to c_languageIntroduction to c_language
Introduction to c_languageWay2itech
 

Plus de Way2itech (6)

Escape sequences
Escape sequencesEscape sequences
Escape sequences
 
Variables in C and C++ Language
Variables in C and C++ LanguageVariables in C and C++ Language
Variables in C and C++ Language
 
Compilation of c
Compilation of cCompilation of c
Compilation of c
 
8. operators
8. operators8. operators
8. operators
 
7. input and output functions
7. input and output functions7. input and output functions
7. input and output functions
 
Introduction to c_language
Introduction to c_languageIntroduction to c_language
Introduction to c_language
 

Dernier

Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdfssuserdda66b
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 

Dernier (20)

Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 

10. switch case

  • 1. Switch – Case Statements By: Er. Aman Kumar
  • 3. • Switch case statement allows us to make decisions from multiple choices. • Integer expression must yield an integer value like 1, 2, 3, etc. The keyword case is followed by an integer or a character constant. • Each constant in each case must be different from all the others. The “do this” lines in the above form of switch represent any valid C statement. way2ITech
  • 5. • break always used with the switch statement to terminate the switch statement or to prevent the entry in other case. way2ITech
  • 8. The Tips and Traps • It is not compulsory to put cases in ascending order as we did above. We can place them in any order we want. way2ITech
  • 9. You are also allowed to use char values in case and switch as shown in the following program (Program to find whether character entered is vowel or not) way2ITech
  • 12. If a statement doesn’t belong to any case the compiler won’t report an error. However, the statement would never get executed. For example, in the following program the printf( ) never goes to work. way2ITech
  • 13. • If there is no default in switch, then error will not be there. If no case is matching, statements after switch will execute. • Relational Expression like i>5 are not allowed in any case. Even float values not allowed in any case • We can check the value of any expression in a switch. Thus the following switch statements are legal. switch ( i - j * k ) switch ( 3 + 9 % 4 * i ) switch ( a < 5 || b > 7 ) way2ITech
  • 14. • Expressions can also be used in cases provided they are constant expressions. Thus case 13 + 7 is correct, however, case a + b is incorrect. • switch may occur within another, but in practice it is rarely done. Such statements would be called nested switch statements. • The switch statement is very useful while writing menu driven programs • Even if there are multiple statements to be executed in each case there is no need to enclose them within a pair of braces (unlike if, and else). way2ITech
  • 15. switch Versus if-else Ladder Disadvantages of switch over if-else • A float expression cannot be tested using a switch • Cases can never have variable expressions (for example it is wrong to say case a +3 : ) • Multiple cases cannot use same expressions. Thus the following switch is illegal: way2ITech
  • 16. Advantages of switch over if-else • compiler generates a jump table for a switch during compilation. As a result, during execution it simply refers the jump table to decide which case should be executed, rather than actually checking which case is satisfied. • As against this, if-else are slower because they are evaluated at execution time. way2ITech