Download presentation
Presentation is loading. Please wait.
Published byStephany Houston Modified over 9 years ago
1
The switch Statement
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. Zaghloul2
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. Zaghloul3
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. Zaghloul4
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. Zaghloul5
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. Zaghloul6
7
Dr. Soha S. Zaghloul7 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 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. Zaghloul8
9
#include 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. Zaghloul9
10
#include 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. Zaghloul10
11
Dr. Soha S. Zaghloul11 #include 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 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. Zaghloul12
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. Zaghloul13
14
#include 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. Zaghloul14
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. Zaghloul15
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.