UMBC CS 201, Fall 98
UMBC CMSC 201 & 201H Fall '98 CSEE | 201 | 201 F'98 | lectures | news | help

Examples using switch

cardrank

The Program

/* * File: cardrank.c * ---------------- * Reads in a number between 1 and 13 and writes * out the appropriate symbol for a playing card * of that rank. */ #include <stdio.h> main() { int n; /* Prompt user and get card rank */ printf("What is the rank of the card (1-13)? "); scanf ("%d", &n); /* Print appropriate name */ switch (n) { case 1: /* print special card names */ printf("Ace\n"); break; case 11: printf("Jack\n"); break; case 12: printf("Queen\n"); break; case 13: printf("King\n"); break; default: /* print card value */ printf("%d\n", n); break; } }

The Sample Run

lassie% a.out What is the rank of the card (1-13)? 11 Jack lassie% a.out What is the rank of the card (1-13)? 1 Ace lassie% a.out What is the rank of the card (1-13)? 8 8 lassie% a.out What is the rank of the card (1-13)? 12 Queen lassie% a.out What is the rank of the card (1-13)? 13 King lassie% a.out What is the rank of the card (1-13)? 7 7 lassie% a.out What is the rank of the card (1-13)? 14 14 lassie%

Oops


cardrank2

The Program

/* * File: card2.c * ---------------- * Reads in a number between 1 and 13 and writes * out the appropriate symbol for a playing card * of that rank. */ #include <stdio.h> main() { int n; /* Prompt user and get card rank */ printf("What is the rank of the card (1-13)? "); scanf ("%d", &n); /* Print appropriate name */ switch (n) { case 1: /* print special card names */ printf("Ace\n"); break; case 11: printf("Jack\n"); break; case 12: printf("Queen\n"); break; case 13: printf("King\n"); break; case 2: /* print card value */ case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10: printf("%d\n", n); break; default: /* handle BAD INPUT */ printf("Bad card number\n"); break; } }

The Sample Run

lassie% a.out What is the rank of the card (1-13)? 1 Ace lassie% a.out What is the rank of the card (1-13)? 8 8 lassie% a.out What is the rank of the card (1-13)? 14 Bad card number lassie% a.out What is the rank of the card (1-13)? 13 King


CSEE | 201 | 201 F'98 | lectures | news | help

Wednesday, 16-Sep-1998 18:22:40 EDT