Solution for Exercise 8-17
ex0817
#include <stdio.h> int main() { int code; printf("Enter the error code (1-3): "); scanf("%d",&code); switch(code) { case 1: puts("Drive Fault, not your fault."); break; case 2: puts("Illegal format, call a lawyer."); break; case 3: puts("Bad filename, spank it."); break; default: puts("That's not 1, 2, or 3"); } return(0); }
Output
Enter the error code (1-3): 3
Bad filename, spank it.
Notes
* The item evaluated by switch must be a single value or expression that results in a single value. It cannot be a string, comparison, logical operation, or function that doesn't return a single value.
* case statements specify single values which are compared with the item in the parentheses after switch. Single characters can be specified, in which case they're enclosed in single quotes.
* break statements are not required. If omitted, execution falls through to the next case statement
* The default item doesn't need a break statement as it's the last item in the structure.
* A case statement can be blank, having no statements belonging to it.
Copyright © 1997-2024 by QPBC.
All rights reserved