#100DaysOfWeb in Python Transcripts
Chapter: Days 25-28: JavaScript Introduction
Lecture: Conditionals and control flow
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
This is a quick video on conditionals and a break in continue statements. First, let's demo a very simple if else.
0:10
Let's use our constant from the previous lesson of mean age close to 18. And that set my age to 17. Now what if you want to determine
0:25
if at my age, I can drive being the driving age, the mean age of 18? Well, it's very simple to write an if statement. So if my age, is greater than
0:39
or equal than mean age I can drive. And that doesn't return anything, because I'm not 18 yet.
0:58
So we can add an else block, to print a different message. And now, that gets me, cannot drive as expected.
1:14
Now there's a shorter way to write this in JavaScript using a ternary statement. So in that case I'm doing the if condition in parentheses.
1:28
And look again at the nice auto complete of the Chrome DevTools console. So the ternary, I put my conditions in parentheses then to question mark
1:38
then you put the code in that hits when it's true. Then you do a colon, and you put a code that hits when it's false. So instead of five lines of code
1:53
I can do this just on one line. That looks pretty nice. Next, let's do something a bit more advanced. So we're going to make a list of names.
2:00
We're going to print the names ignoring the ones that start with B and stopping when we hit a name that starts with Q. So here is an array of names.
2:11
Let me use what I had before but add a few names starting with B one that starts with Q, where we want to stop and two names at the end.
2:29
So an array of seven names I'm going to look over with a new syntax. Right, that gets them all seven. And next I'm going to
2:43
ignore the ones that start with B. So we can write an if statement. If name starts with then we should recognize that from Python.
3:12
We only get the ones with B. One in this case. I want to continue. So I can use the continue statement. So now I got five names
3:27
because the one starting with B are omitted. Lastly we said, if we hit a name with Q we stopped a loop, and we can use a break.
3:41
So if you have a second condition we can use else if. And that condition will be the name starts with Q.
4:04
I'm going to break out of the loop. And now we have only two names because Bob was ignored, Tuni and Mike were printed Berta was ignored.
4:14
Then we found Quintin and we broke out of the loop. So again, as simple as else. If my age is greater and equals and mean age they can drive.
4:25
Otherwise I cannot drive. And as my age is less than mean age cannot drive was printed. I can write the shorter using ternary.
4:36
A nice construct in JavaScript puts condition in parentheses, followed by a question mark. Then you have your true code.
4:45
Colon decoded hits when it's false. An example of using if else and for loop. I got a bunch of names. I'm going to ignore names that start with B
4:58
and stop when we hit a name starting with Q. First we have our loop. Then we have an if else if else.
5:08
And actually my console lock defaulted after here. But here I put it in an else. Should have the same effect. If the name starts with B
5:18
I will have to continue to look without doing anything. Else if, the name starts with Q I'm going to break out to look
5:24
and effectively stops this program. If it's neither of those I have to print a name. And we saw that Mike and Jillian are the only ones left.
5:36
It's just an example to show you how you can combine four if else with continue upgrade to manage, to control flow.
5:44
Now there are two major building blocks left when it comes down to JavaScript basics which are functions and object.
5:51
And I will cover those in the next two videos.