SlideShare a Scribd company logo
1 of 15
The switch Statement
 Occasionally, an algorithm will contain a series of
decisions in which a variable or expression is tested
separately for each of the constant integral values it
may assume, and different actions are taken.
 This is called multiple selection.
 C provides the switch multiple-selection statement to
handle such decision making.
 The switch statement consists of a series of case
labels, an optional default case and statements to
execute for each case.
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
Dr. Soha S. Zaghloul 2
switch (identifier) // must be a constant expressions integer or
char
{
case ‘value1’: block of statements 1;
break;
case ‘value2’: block of statements 2;
break;
…
…
case ‘value n’: block of statements n;
break;
default: block of statements;
break;
}
Dr. Soha S. Zaghloul 3
 break is used to exit the switch statement.
 default is used if the variable did not satisfy any
value of the listed cases.
Dr. Soha S. Zaghloul 4
 Write a program that displays a menu and prints
a message for each selection made by the user.
The program should prompt the user in case of
an invalid input.
 The menu to be displayed is as follows:
Enter your choice:
‘E’: Edit my program
‘C’: Compile my program
‘R’: Run my program
Dr. Soha S. Zaghloul 5
1. Display the menu
2. Get the input from the user
3. If the user entered ‘E’, then print “Calling the editor”
4. If the user entered ‘C’, then print “Calling the compiler”
5. If the user entered ‘R’, then print “The program starts execution”
6. For any other input, print “Invalid input”
ALGORITHM
Dr. Soha S. Zaghloul 6
Dr. Soha S. Zaghloul 7
START
Display
Menu
READ
choice
choice
= ‘E’
choice
= ‘C’
choice
= ‘R’
Print “Call
Editor”
Print “Call
Compiler”
Print “The program
starts execution”
Print “Invalid
Input”
END
TRUE
TRUE
TRUE
FALSE
FALSE
FALSE
1. Display the menu
2. Scanf choice
3. Switch for the value of choice:
a. Case choice = ‘E’ printf (“Calling the Editor”)
b. Break
c. Case choice = ‘C’ printf (“Calling the Compiler”)
d. Break
e. Case choice = ‘R’ printf (“The program starts execution”)
f. Break
g. Otherwise printf (“Invalid input”);
4. End of program
PSEUDOCODE
Dr. Soha S. Zaghloul 8
#include <stdio.h>
int main (void)
{
printf (“Enter your choice n”);
printf (“E: Edit n”);
printf (“C: Compile n”);
printf (“R: Run n”);
printf (“What do you want to do? “);
}
1. Display the menu
2. Scanf choice
3. Switch for the value of choice:
a. Case choice = ‘E’ printf (“Calling the
Editor”)
b. Break
c. Case choice = ‘C’ printf (“Calling the
Compiler”)
d. Break
e. Case choice = ‘R’ printf (“The
program starts execution”)
f. Break
g. Otherwise printf (“Invalid input”);
4. End of program
PSEUDOCODE
Dr. Soha S. Zaghloul 9
#include <stdio.h>
int main (void)
{
char choice;
printf (“Enter your choice n”);
printf (“E: Edit n”);
printf (“C: Compile n”);
printf (“R: Run n”);
printf (“What do you want to do? “);
scanf (“%c”, &choice);
}
1. Display the menu
2. Scanf choice
3. Switch for the value of choice:
a. Case choice = ‘E’ printf (“Calling the
Editor”)
b. Break
c. Case choice = ‘C’ printf (“Calling the
Compiler”)
d. Break
e. Case choice = ‘R’ printf (“The
program starts execution”)
f. Break
g. Otherwise printf (“Invalid input”);
4. End of program
PSEUDOCODE
Dr. Soha S. Zaghloul 10
Dr. Soha S. Zaghloul 11
#include <stdio.h>
int main (void)
{
char choice;
printf (“Enter your choice n”);
printf (“E: Edit n”);
printf (“C: Compile n”);
printf (“R: Run n”);
printf (“What do you want to do? n“);
scanf (“%c”, &choice);
switch (choice)
{
case ‘E’: printf (“Calling the Editor n”);
break;
case ‘C’: printf (“Calling the Compiler n”);
break;
case ‘R’: printf (“The program starts execution n”);
break;
default: printf (“Invalid Input n”);
break;
} // end switch
}
1. Display the menu
2. Scanf choice
3. Switch for the value of choice:
a. Case choice = ‘E’ printf (“Calling the
Editor”)
b. Break
c. Case choice = ‘C’ printf (“Calling the
Compiler”)
d. Break
e. Case choice = ‘R’ printf (“The
program starts execution”)
f. Break
g. Otherwise printf (“Invalid input”);
4. End of program
PSEUDOCODE
#include <stdio.h>
int main (void)
{
char choice;
printf (“Enter your choice n”);
printf (“E: Edit n”);
printf (“C: Compile n”);
printf (“R: Run n”);
printf (“What do you want to do? n“);
scanf (“%c”, &choice);
switch (choice)
{
case ‘E’: printf (“Calling the Editor n”);
break;
case ‘C’: printf (“Calling the Compiler n”);
break;
case ‘R’: printf (“The program starts execution n”);
break;
default: printf (“Invalid Input n”);
break;
} // end switch
return (0);
} // end of main
1. Display the menu
2. Scanf choice
3. Switch for the value of choice:
a. Case choice = ‘E’ printf (“Calling the
Editor”)
b. Break
c. Case choice = ‘C’ printf (“Calling the
Compiler”)
d. Break
e. Case choice = ‘R’ printf (“The
program starts execution”)
f. Break
g. Otherwise printf (“Invalid input”);
4. End of program
PSEUDOCODE
Dr. Soha S. Zaghloul 12
 Sometimes, the same actions are to be
performed on two different values.
 For example, capital and small letters in the
previous example.
 The code will be then updated as follows:
Dr. Soha S. Zaghloul 13
#include <stdio.h>
int main (void)
{
char choice;
printf (“Enter your choice n”);
printf (“E: Edit n”);
printf (“C: Compile n”);
printf (“R: Run n”);
printf (“What do you want to do? n“);
scanf (“%c”, &choice);
switch (choice)
{
case ‘E’:
case ‘e’: printf (“Calling the Editor n”);
break;
case ‘C’:
case ‘c’: printf (“Calling the Compiler n”);
break;
case ‘R’:
case ‘r’: printf (“The program starts execution n”);
break;
default: printf (“Invalid Input n”);
break;
} // end switch
return (0);
} // end of main
Case choice = ‘E’ or ‘e’ printf
(“Calling the Editor n”)
The default part is optional
Dr. Soha S. Zaghloul 14
 Strings CANNOT be used as labels (cases) in a switch
statement.
 The following code is WRONG because name is a
string.
char name[20];
printf (“Enter your first name: n”);
scanf (“%s”, name);
switch (name)
{
case “Ahmad”: printf (“Ahmad is a nice boy n”);
break;
case “Laila” : printf (“Laila is a nice girl n”);
break;
}
Dr. Soha S. Zaghloul 15

More Related Content

Similar to week4_lec2_switch-statement.pptx

Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02Suhail Akraam
 
Fundamentals of programming)
Fundamentals of programming)Fundamentals of programming)
Fundamentals of programming)jakejakejake2
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in Csana shaikh
 
Core programming in c
Core programming in cCore programming in c
Core programming in cRahul Pandit
 
Fundamental of C Programming Language and Basic Input/Output Function
  Fundamental of C Programming Language and Basic Input/Output Function  Fundamental of C Programming Language and Basic Input/Output Function
Fundamental of C Programming Language and Basic Input/Output Functionimtiazalijoono
 
Fundamentals of prog. by rubferd medina
Fundamentals of prog. by rubferd medinaFundamentals of prog. by rubferd medina
Fundamentals of prog. by rubferd medinarurumedina
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CSowmya Jyothi
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Shipra Swati
 
BHARGAVISTATEMENTS.PPT.pptx
BHARGAVISTATEMENTS.PPT.pptxBHARGAVISTATEMENTS.PPT.pptx
BHARGAVISTATEMENTS.PPT.pptxSasideepa
 

Similar to week4_lec2_switch-statement.pptx (20)

Elements of programming
Elements of programmingElements of programming
Elements of programming
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
 
Fundamentals of programming)
Fundamentals of programming)Fundamentals of programming)
Fundamentals of programming)
 
Presentation1
Presentation1Presentation1
Presentation1
 
CP Handout#6
CP Handout#6CP Handout#6
CP Handout#6
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
C Basics
C BasicsC Basics
C Basics
 
Decision Making and Branching
Decision Making and BranchingDecision Making and Branching
Decision Making and Branching
 
Core programming in c
Core programming in cCore programming in c
Core programming in c
 
Fundamental of C Programming Language and Basic Input/Output Function
  Fundamental of C Programming Language and Basic Input/Output Function  Fundamental of C Programming Language and Basic Input/Output Function
Fundamental of C Programming Language and Basic Input/Output Function
 
Lec 10
Lec 10Lec 10
Lec 10
 
Fundamentals of prog. by rubferd medina
Fundamentals of prog. by rubferd medinaFundamentals of prog. by rubferd medina
Fundamentals of prog. by rubferd medina
 
7 functions
7  functions7  functions
7 functions
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in C
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8
 
C programs
C programsC programs
C programs
 
BHARGAVISTATEMENTS.PPT.pptx
BHARGAVISTATEMENTS.PPT.pptxBHARGAVISTATEMENTS.PPT.pptx
BHARGAVISTATEMENTS.PPT.pptx
 
C
CC
C
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Hargun
HargunHargun
Hargun
 

Recently uploaded

Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
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
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
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
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 

Recently uploaded (20)

Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.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...
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
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
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 

week4_lec2_switch-statement.pptx

  • 2.  Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each of the constant integral values it may assume, and different actions are taken.  This is called multiple selection.  C provides the switch multiple-selection statement to handle such decision making.  The switch statement consists of a series of case labels, an optional default case and statements to execute for each case. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. Dr. Soha S. Zaghloul 2
  • 3. switch (identifier) // must be a constant expressions integer or char { case ‘value1’: block of statements 1; break; case ‘value2’: block of statements 2; break; … … case ‘value n’: block of statements n; break; default: block of statements; break; } Dr. Soha S. Zaghloul 3
  • 4.  break is used to exit the switch statement.  default is used if the variable did not satisfy any value of the listed cases. Dr. Soha S. Zaghloul 4
  • 5.  Write a program that displays a menu and prints a message for each selection made by the user. The program should prompt the user in case of an invalid input.  The menu to be displayed is as follows: Enter your choice: ‘E’: Edit my program ‘C’: Compile my program ‘R’: Run my program Dr. Soha S. Zaghloul 5
  • 6. 1. Display the menu 2. Get the input from the user 3. If the user entered ‘E’, then print “Calling the editor” 4. If the user entered ‘C’, then print “Calling the compiler” 5. If the user entered ‘R’, then print “The program starts execution” 6. For any other input, print “Invalid input” ALGORITHM Dr. Soha S. Zaghloul 6
  • 7. Dr. Soha S. Zaghloul 7 START Display Menu READ choice choice = ‘E’ choice = ‘C’ choice = ‘R’ Print “Call Editor” Print “Call Compiler” Print “The program starts execution” Print “Invalid Input” END TRUE TRUE TRUE FALSE FALSE FALSE
  • 8. 1. Display the menu 2. Scanf choice 3. Switch for the value of choice: a. Case choice = ‘E’ printf (“Calling the Editor”) b. Break c. Case choice = ‘C’ printf (“Calling the Compiler”) d. Break e. Case choice = ‘R’ printf (“The program starts execution”) f. Break g. Otherwise printf (“Invalid input”); 4. End of program PSEUDOCODE Dr. Soha S. Zaghloul 8
  • 9. #include <stdio.h> int main (void) { printf (“Enter your choice n”); printf (“E: Edit n”); printf (“C: Compile n”); printf (“R: Run n”); printf (“What do you want to do? “); } 1. Display the menu 2. Scanf choice 3. Switch for the value of choice: a. Case choice = ‘E’ printf (“Calling the Editor”) b. Break c. Case choice = ‘C’ printf (“Calling the Compiler”) d. Break e. Case choice = ‘R’ printf (“The program starts execution”) f. Break g. Otherwise printf (“Invalid input”); 4. End of program PSEUDOCODE Dr. Soha S. Zaghloul 9
  • 10. #include <stdio.h> int main (void) { char choice; printf (“Enter your choice n”); printf (“E: Edit n”); printf (“C: Compile n”); printf (“R: Run n”); printf (“What do you want to do? “); scanf (“%c”, &choice); } 1. Display the menu 2. Scanf choice 3. Switch for the value of choice: a. Case choice = ‘E’ printf (“Calling the Editor”) b. Break c. Case choice = ‘C’ printf (“Calling the Compiler”) d. Break e. Case choice = ‘R’ printf (“The program starts execution”) f. Break g. Otherwise printf (“Invalid input”); 4. End of program PSEUDOCODE Dr. Soha S. Zaghloul 10
  • 11. Dr. Soha S. Zaghloul 11 #include <stdio.h> int main (void) { char choice; printf (“Enter your choice n”); printf (“E: Edit n”); printf (“C: Compile n”); printf (“R: Run n”); printf (“What do you want to do? n“); scanf (“%c”, &choice); switch (choice) { case ‘E’: printf (“Calling the Editor n”); break; case ‘C’: printf (“Calling the Compiler n”); break; case ‘R’: printf (“The program starts execution n”); break; default: printf (“Invalid Input n”); break; } // end switch } 1. Display the menu 2. Scanf choice 3. Switch for the value of choice: a. Case choice = ‘E’ printf (“Calling the Editor”) b. Break c. Case choice = ‘C’ printf (“Calling the Compiler”) d. Break e. Case choice = ‘R’ printf (“The program starts execution”) f. Break g. Otherwise printf (“Invalid input”); 4. End of program PSEUDOCODE
  • 12. #include <stdio.h> int main (void) { char choice; printf (“Enter your choice n”); printf (“E: Edit n”); printf (“C: Compile n”); printf (“R: Run n”); printf (“What do you want to do? n“); scanf (“%c”, &choice); switch (choice) { case ‘E’: printf (“Calling the Editor n”); break; case ‘C’: printf (“Calling the Compiler n”); break; case ‘R’: printf (“The program starts execution n”); break; default: printf (“Invalid Input n”); break; } // end switch return (0); } // end of main 1. Display the menu 2. Scanf choice 3. Switch for the value of choice: a. Case choice = ‘E’ printf (“Calling the Editor”) b. Break c. Case choice = ‘C’ printf (“Calling the Compiler”) d. Break e. Case choice = ‘R’ printf (“The program starts execution”) f. Break g. Otherwise printf (“Invalid input”); 4. End of program PSEUDOCODE Dr. Soha S. Zaghloul 12
  • 13.  Sometimes, the same actions are to be performed on two different values.  For example, capital and small letters in the previous example.  The code will be then updated as follows: Dr. Soha S. Zaghloul 13
  • 14. #include <stdio.h> int main (void) { char choice; printf (“Enter your choice n”); printf (“E: Edit n”); printf (“C: Compile n”); printf (“R: Run n”); printf (“What do you want to do? n“); scanf (“%c”, &choice); switch (choice) { case ‘E’: case ‘e’: printf (“Calling the Editor n”); break; case ‘C’: case ‘c’: printf (“Calling the Compiler n”); break; case ‘R’: case ‘r’: printf (“The program starts execution n”); break; default: printf (“Invalid Input n”); break; } // end switch return (0); } // end of main Case choice = ‘E’ or ‘e’ printf (“Calling the Editor n”) The default part is optional Dr. Soha S. Zaghloul 14
  • 15.  Strings CANNOT be used as labels (cases) in a switch statement.  The following code is WRONG because name is a string. char name[20]; printf (“Enter your first name: n”); scanf (“%s”, name); switch (name) { case “Ahmad”: printf (“Ahmad is a nice boy n”); break; case “Laila” : printf (“Laila is a nice girl n”); break; } Dr. Soha S. Zaghloul 15