September 1998
Intermediate to advanced
848 pages
20h 13m
English
The conditional operator and the if else construction make it easy to write programs that choose between two alternatives. Sometimes, however, a program needs to choose one of several alternatives. You can do this by using if else if…else, but in many cases it is more convenient to use the C switch statement. Listing 7.11 is an example showing how the switch statement works. This program reads in a letter and then responds by printing an animal name that begins with that letter.
/* animals.c -- uses a switch statement */ #include <stdio.h> #include <ctype.h> int main(void) { char ch; printf("Give me a letter of the alphabet, and I will give "); printf("an animal name\nbeginning ... |
Read now
Unlock full access