From the course: C Essential Training

Working with the switch-case structure - C Tutorial

From the course: C Essential Training

Start my 1-month free trial

Working with the switch-case structure

- [Instructor] This code shows a switch case structure, which is yet another way to alter program flow in C. This construction uses the C language keywords switch, case, and default. It forms. what's called a selection statement. You also frequently see the break keyword associated with this structure. The switch case structure alters program flow like an if else, if else type of construction, but it behaves differently. Unlike if, the switch statement doesn't evaluate a condition instead it's followed by an expression that results in a single value. Typically, it's just a variable, such as variable a, a shown here at line 10, again, this isn't a comparison. What follows are case statements formatted as you see here, all enclosed in braces. The case statement is where the comparison takes place. The value specified by switch is compared with a constant specified after case. This is followed by a colon. If there's a match between these constants and this expression, the statements belonging to case are executed. Otherwise they're skipped and the next case statement is evaluated. And again, finally, to the default statement, which catches what's left. Evaluation is also altered by the break keyword, which breaks program flow past the switch statement's final curly brace. In this code, the user is prompted to enter a value, one, two or three. The switch statement uses the value input as its expression. When the value is one, at line 12, the puts statement executes, then the break keyword escapes from the structure. Otherwise, execution falls through to the next case statement, compared with two, and then three and if nothing matches at this point, default takes over. It's statement is executed and the construction ends. Build and run the code. And I'll type one, and it was the first item. Let's run again, and go ahead and choose item number three. Very good. And now let's run and type something a little weird, negative 16, invalid choice. The brake statements aren't a required part of the switch case construction, they're added so that execution doesn't fall through to all the options. I'll delete these brake statements to show you the effect it has on the code. Save, build and run. And now I'll type option one. And you can see that all the put statements output their text. The switch case structure in this exercise file uses character variables to compare items input from a menu. Build and run. Let's type a for bugs. Whoops, see, you must type a capital A for the code to run. It would be nice, however, if the code caught the lowercase letters as well. In this improvement to the code, I've added extra case statements to account for lowercase letters. Better formatting. Because execution falls through a case statement, there's no break here. This case statement will catch big A, this one little A, and the result will output these statements. Build and run. And now I'm going to type a little a, and it worked. Now you can order eyeball soup by pressing big C or little c. And of course, that's what the customer wants. Remember, the case statements always fall through. If you forget a break, which isn't technically part of the switch case structure, execution will fall through. You can use this to your advantage as shown in this code. Otherwise if things screw up, remember you need that break to keep the flow from falling through the entire structure.

Contents