The switch-case structure is a selection statement based on a single value and comparison made with subsequent statements and corresponding blocks of code executed when the comparison is true. Use a switch-case structure to evaluate multiple possibilities, especially when such tedious comparisons otherwise result in a messy series of if-else-if statements.
- [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.
- Writing your first C program
- Working the C development cycle
- Adding comments to code for clarity
- C language data types
- Declaring variables
- Making new data types
- Typecasting variables
- Using math, assignment, and logical operators
- Creating for and while loops
- Nesting loops
- Creating functions
- Returning values from functions
- Passing arguments to functions
- Creating recursive functions
Skill Level Intermediate
C: Data Structures, Pointers, and File Systemswith Dan Gookin2h 57m Intermediate
1. The Fundamentals of C
2. Variables and Values
3. Operators and Math
4. Decisions and Loops
Next steps1m 38s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.