Decision Branching – switch/case
Lab: Building Blocks of Programming
Video Runtime: 07:47
There are times when you need to make multiple decisions. In these cases, the level of branches grows. Programming languages give you an alternative with the switch/case
construct.
Your key takeaways are:
- Switch/case more concise representation of an if/else if/else conditional control statement
- Don’t have to repeat the conditional comparison in each branch
- Easier to read
- Gives you control to determine what code to run (execute)
Study Notes
Below is some pseudocode to show a multistate decision tree:
if ( weatherCondition == 'sunny' ) | |
go for a walk | |
else if ( weatherCondition == 'raining' ) | |
dance in the rain | |
else if ( weatherCondition == 'foggy' ) | |
sound the fog horn | |
else if ( weatherCondition == 'snowing' ) | |
build a snowman | |
else | |
read a book |
Convert the multi-state decision tree into a less verbose format with a switch/case statement:
switch ( weatherCondition ) | |
case 'sunny': | |
go for a walk | |
break; | |
case 'raining': | |
dance in the rain | |
break; | |
case 'foggy': | |
sound the fog horn | |
break; | |
case 'snowing': | |
build a snowman | |
break; | |
default: | |
read a book |
Anatomy of the Switch Statement
switch ( expression ) // expression's value is matched to each of the case statements. | |
case value1: // If the expression matches this value, then do this block of code. | |
// do something | |
break // stop and jump out of the switch | |
case value2: | |
// do something | |
break | |
case value3: | |
// do something | |
break | |
default: // "else" catch all code block If none of the others match, then do this one. | |
// do something |
Switch Statements in Other Languages
switch ( weatherCondition ) { | |
case 'sunny': | |
printf("go for a walk"); | |
break; | |
case 'raining': | |
printf("dance in the rain"); | |
break; | |
case 'foggy': | |
printf("sound the fog horn"); | |
break; | |
case 'snowing': | |
printf("build a snowman"); | |
break; | |
default: | |
printf("read a bookâ); | |
} |
switch ( weatherCondition ) { | |
case "sunny": | |
document.write("go for a walk"); | |
break; | |
case raining": | |
document.write("dance in the rain"); | |
break; | |
case "foggy: | |
document.write("sound the fog horn"); | |
break; | |
case "snowing": | |
document.write("build a snowman"); | |
break; | |
default: | |
document.write("read a bookâ); | |
} |
switch ( $weatherCondition ) { | |
case 'sunny': | |
echo 'go for a walk'; | |
break; | |
case 'raining': | |
echo 'dance in the rain'; | |
break; | |
case 'foggy': | |
echo 'sound the fog horn; | |
break; | |
case 'snowing': | |
echo 'build a snowman'; | |
break; | |
default: | |
echo 'read a book'; | |
} |
Your best friend is code, Tonya is making the introductions.
Episodes
Total Lab Runtime: 02:17:55
- 1 Lab Introductionfree 06:21
- 2 User-Friendly Expression of Informationfree 22:42
- 3 Fundamentals of Syntaxfree 08:57
- 4 Decision Branching - if/then/elsefree 10:39
- 5 Decision Branching – switch/casefree 07:47
- 6 Basics of Iterationfree 14:14
- 7 Iteration in Practicefree 12:51
- 8 Order of Executionfree 10:55
- 9 Abstractionfree 11:45
- 10 Scoping – Who Can Communicate With Whomfree 31:44