Presentation is loading. Please wait.

Presentation is loading. Please wait.

Switch statement.

Similar presentations


Presentation on theme: "Switch statement."— Presentation transcript:

1 Switch statement

2 Outline In this lesson, we will: Introduce the switch statement
Learn how it works Contrasted with cascading conditional statements See an overview of how it is implemented

3 Cascading conditional statements
There are many cases where you may need to perform an action based on a state: if ( state == 0 ) { // Do something... } else if ( state == 1 ) { // Do something else... } else if ( state == 2 ) { // Do something else yet again... } else { // Perform some default action... }

4 States Here are two situations:
In the mathematical application package Maple, there is one such case where there are over 200 alternative states Operating systems (oss) also rely on states, and decisions are often made based on those states E.g., react depending on what the os is currently dealing with: Bus error, illegal instruction, trace, input-output transfer, emulation, system call, floating-point exception, or segmentation exception E.g., react to a character: An ordinary character, a non-printing character, a backspace, a new line, a tab, vertical motion or carriage return

5 Switch statements If both these conditions hold:
The state is encoded using an integer datatype, and All the possible cases are known prior to compilation, a switch statement can be used in place of cascading conditional statements switch ( state ) { case 0: // Do something break; case 1: // Do something else... case 2: // Do something else yet again... default: // Perform some default action... }

6 Switch statements Some points:
The cases must be literals (e.g., -1, 0, 1, 2, 3, … or 'a', 'b', 'c', …) The break statement jumps to the end of the switch statement Any state not explicitly listed as a case results in the execution of the default case The default case is: Entirely optional If it is not listed, nothing is done for anything other than the specified cases Usually listed last, but can also appear anywhere in the list of cases

7 Morse code example For example, in converting English to Morse code:
char ch{}; // Get a character value... switch ( ch ) { case 'A': result = ". ___"; break; case 'B': result = "___ . . ."; break; case 'C': result = "___ . ___ ."; break; case 'D': result = "___ . ."; break; case 'E': result = "."; break; case 'F': result = ". . ___ ."; break; case 'G': result = "___ ___ ."; break; case 'H': result = " "; break; . case 'Z': result = "___ ___ . ."; break; }

8 A subtle point with break
If the break statement is not included, it will continue executing the code for subsequent statements: For example, the code executed for case 0 only ends a break statement is reached switch ( state ) { case 0: // Do something... break; case 1: // Do something else... case 2: // Do something else yet again... default: // Perform a default action... } switch ( state ) { case 0: // Do something... case 1: // Do something else... break; case 2: // Do something else yet again... default: // Perform a default action... }

9 Vowels versus consonants
Consider possibly dealing with either vowels or consonants: char ch{}; // Get a character value... if ( (ch >= 'A') && (ch <= 'Z') ) { switch ( ch ) { case 'A': case 'E': case 'I': case 'O': case 'U': case 'Y': bool consonant_y{false}; // Do something, and determine if any 'y' is actually a // consonant in the word in question if ( !consonant_y ) { break; } default: // Do something for a consonant

10 Why a switch statement? Why introduce a switch statement?
If possible, the compiler will simplify execution by not checking each case individually, but instead by creating a look-up table Hidden from you, a table will be created indexed by the type you indicated switch ( state ) { case 0: // Do something... case 1: // Do something else... break; case 2: // Do something else yet again... default: // Perform a default action... }

11 Summary Following this lesson, you now Understand the switch statement
Know that it is based on an expression with an integer value The cases must be literals Understand that this allows for more efficient implementations than cascading conditional statements

12 References [1] C++ Reference [2] Wikipedia [3] cplusplus.com

13 Colophon These slides were prepared using the Georgia typeface. Mathematical equations use Times New Roman, and source code is presented using Consolas. The photographs of lilacs in bloom appearing on the title slide and accenting the top of each other slide were taken at the Royal Botanical Gardens on May 27, 2018 by Douglas Wilhelm Harder. Please see for more information.

14 Disclaimer These slides are provided for the ece 150 Fundamentals of Programming course taught at the University of Waterloo. The material in it reflects the authors’ best judgment in light of the information available to them at the time of preparation. Any reliance on these course slides by any party for any other purpose are the responsibility of such parties. The authors accept no responsibility for damages, if any, suffered by any party as a s_result of decisions made or actions based on these course slides for any other purpose than that for which it was intended.


Download ppt "Switch statement."

Similar presentations


Ads by Google