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); }
Notes
* The item specified by switch must be a single value. It cannot be a string or anything other than a single value. Functions that return a single value are okay, but a comparison or logical expression doesn't belong.
* A case statement specifies a single value which is compared with the item in the parentheses after switch. Single characters can be specified, in which case they're enclosed in single quotes.
* The break statement is not required. If it's 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.
* The default statement can be blank. Heck, a case statement can be blank as well.
Copyright © 1997-2024 by QPBC.
All rights reserved