Decision Branching – if/then/else
Lab: Building Blocks of Programming
Video Runtime: 10:39
Let’s talk about decision branching and more specifically about the if/then/else
constructs. Decisions control the flow of your program.
Your key takeaways are:
- Conditional control statements determine the flow of the program
- if/then
- if/else
- if/else if/else
- The combination of the conditionals must evaluate to true for the if or else if to be true
Study Notes
The basic structure of a if/then branching code block is:
if ( conditions ) then do something
The basic structure of a if/else branching code block is:
if ( conditions ) then do something
else do this instead
These allows you to set the conditions. But, what if the conditions are not set? What else can you do?
If you need more than one control path:
if ( conditions ) then do something
else if ( conditions ) then do that
else do this instead
If/Then in Languages
Review the if/then statements in C, PHP, Python, and JavaScript below. Do you notice the similarities?
C:
if ( weatherIsSunny && ! weatherIsRaining && ! isWorking ) { | |
printf( "Going to the festival" ); | |
} else if ( movieIsPlaying ) { | |
printf( "Going to the movies" ); | |
} else { | |
printf( "Staying home" ); | |
} |
PHP:
<?php | |
if ( $weatherIsSunny && ! $weatherIsRaining && ! $isWorking ) { | |
echo 'Going to the festival'; | |
} else if ( $movieIsPlaying ) { | |
echo 'Going to the movies'; | |
} else { | |
echo 'Staying home'; | |
} |
Python:
if weatherIsSunny and not weatherIsRaining and not isWorking : | |
print('Going to the festival') | |
elif ( movieIsPlaying ) : | |
print('Going to the movies') | |
else : | |
print('Staying home') |
JavaScript:
if ( weatherIsSunny && ! weatherIsRaining && ! isWorking ) { | |
console.log( "Going to the festival" ); | |
} else if ( movieIsPlaying ) { | |
console.log( "Going to the movies" ); | |
} else { | |
console.log( "Staying home" ); | |
} |
Hands off the keyboard. Web development starts by thinking first, then planning it out, and then coding it.
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