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