Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C For Dummies 2nd Ed 2004.pdf
Скачиваний:
43
Добавлен:
17.08.2013
Размер:
8.34 Mб
Скачать

Chapter 19

Switch Case, or, From ‘C’ to Shining ‘c’

In This Chapter

Solving the endless else-if puzzle

Using switch-case

Creating a switch-case structure

Honestly, I don’t believe that switch-case is really a loop. But the word loop works so much better than my alternative, structure thing. That’s

because the statements held inside the switch-case structure thing aren’t really repeated, yet in a way they are. Well, anyway.

This chapter uncovers the final kind-of-loop thing in the C language, which is called switch-case. It’s not so much a loop as it’s a wonderful method of cleaning up a potential problem with multiple if statements. As is true with most things in a programming language, it’s just better for me to show you an example than to try to explain it. That’s what this chapter does.

The Sneaky switch-case Loops

Let’s all go to the lobby,

Let’s all go to the lobby,

Let’s all go to the lobby,

And get ourselves a treat!

— Author unknown

And, when you get to the lobby, you probably order yourself some goodies from the menu. In fact, management at your local theater has just devised an

240 Part III: Giving Your Programs the Ability to Run Amok

interesting computer program to help cut down on pesky, hourly-wage employ­ ees. The program they have devised is shown right here:

/* Theater lobby snack bar program */

#include <stdio.h>

int main()

{

char c;

printf(“Please make your treat selection:\n”); printf(“1 - Beverage.\n”);

printf(“2 - Candy.\n”); printf(“3 - Hot dog.\n”); printf(“4 - Popcorn.\n”); printf(“Your choice:”);

/* Figure out what they typed in. */

c=getchar();

if(c==’1’)

printf(“Beverage\nThat will be $8.00\n”); else if(c==’2’)

printf(“Candy\nThat will be $5.50\n”); else if(c==’3’)

printf(“Hot dog\nThat will be $10.00\n”); else if(c==’4’)

printf(“Popcorn\nThat will be $7.50\n”); else

{

printf(“That is not a proper selection.\n”); printf(“I’ll assume you’re just not hungry.\n”); printf(“Can I help whoever’s next?\n”);

}

return(0);

}

Type this source code into your editor. Save it to disk as LOBBY1.C. This should brighten your heart because you know that more LOBBY programs are on the way. . . .

Compile. Fix any errors. You may get a few because it’s such a long program. Watch your spelling and remember your semicolons. Recompile after fixing any errors.

Chapter 19: Switch Case, or, From ‘C’ to Shining ‘c’ 241

Run:

Please make your treat selection: 1 - Beverage.

2 - Candy.

3 - Hot dog.

4 - Popcorn. Your choice:

Press 2, for Candy. Love them Hot Tamales! You see

Candy

That will be $5.50

Gadzooks! For Hot Tamales? I’m sneaking food in next time. . . .

Run the program again and try a few more options. Then, try an option not on the list. Type M for a margarita:

That is not a proper selection.

I’ll assume you’re just not hungry.

Can I help whoever’s next?

Oh, well.

The switch-case Solution to the LOBBY Program

Don’t all those else-if things in the LOBBY1.C program look funny? Doesn’t it appear awkward? Maybe not. But it is rather clumsy. That’s because you have a better way to pick one of several choices in C. What you need is a switch-case loop.

Right away, I need to tell you that switch-case isn’t really a loop. Instead, it’s a selection statement, which is the official designation of what an if state­ ment is. switch-case allows you to select from one of several items, like a long, complex string of if statements — the kind that’s now pestering the LOBBY1.C program.

242 Part III: Giving Your Programs the Ability to Run Amok

Next is the source code for LOBBY2.C, an internal improvement to the LOBBY1.C program. It’s internal because you’re just messing with the program’s guts here — making them more elegant. Externally, the program still works the same:

/* Theater lobby snack bar program */

#include <stdio.h>

int main()

{

char c;

printf(“Please make your treat selection:\n”); printf(“1 - Beverage.\n”);

printf(“2 - Candy.\n”); printf(“3 - Hot dog.\n”); printf(“4 - Popcorn.\n”); printf(“Your choice:”);

/* Figure out what they typed in. */

c=getchar();

switch(c)

{

case ‘1’:

printf(“Beverage\nThat will be $8.00\n”); break;

case ‘2’:

printf(“Candy\nThat will be $5.50\n”); break;

case ‘3’:

printf(“Hot dog\nThat will be $10.00\n”); break;

case ‘4’:

printf(“Popcorn\nThat will be $7.50\n”); break;

default:

printf(“That is not a proper selection.\n”); printf(“I’ll assume you’re just not hungry.\n”); printf(“Can I help whoever’s next?\n”);

}

return(0);

}

Keep the LOBBY1.C program in your editor. Use this source code for LOBBY2.C as a guide and edit what you see on your screen.