HP C A.06.05 Reference Manual
Statements
switch
Chapter 6180
Associating Statements with Multiple case Values
Sometimes you want to associate a group of statements with more than one case value. To
obtain this behavior, you can enter consecutive case labels. The following function, for
instance, returns 1 if the argument is a punctuation character, or 0 if it is anything else.
/* This function returns 1 if the argument is a
* punctuation character. Otherwise, it returns 0.
*/
is_punc(char arg)
{
switch (arg) {
case '.':
case ',':
case ':':
case ';':
case '?':
case '-':
case '(':
case ')':
case '!': return 1;
default : return 0;
}
}
Example
/* Use the switch statement to decide which comment should be printed */
#include <stdio.h>
int main(void)
{
char answer, grade;
answer = 'y';
printf("\n\n");
while (answer == 'y' || answer == 'Y') {
printf("Enter student's grade: ");
fflush(stdin);
scanf("%c", &grade);
printf("\nComments: ");
switch (grade) {
case 'A':
case 'a':
printf("Excellent\n");