Loading
Current section: 4. Control Flow 8 exercises
lesson

Intro to Control Flow

Loading lesson

Transcript

00:00 Hey. Welcome to the control flow exercise of the workshop. I'm excited about this because so far, we've been able to get our programs to execute line by line. Now, we're gonna get them to be able to skip. We'll get them, to be able to repeat the same lines over and over again.

00:15 It's gonna be a lot of fun and a little bit dangerous because sometimes if you tell a computer to repeat the same lines over and over again, how does it know when to stop? Maybe it'll never stop. You know, just keep on going. It's crazy. So you do have to be thoughtful about this stuff.

00:27 So let's talk about a couple of the things, give you a little preview of what you're gonna be working with, and then you can get right into the exercise. So, first, we're gonna talk about conditionals. You've got, this, condition right here and then you have the consequent right here. The if statement, this if is a keyword in JavaScript and you wrap, this condition inside of parentheses and then you have, these curly braces that, creates a block. So anything inside of this block, variables you declare and stuff, will be just scoped down to that block.

00:57 They won't leak out into other parts of your code, which is quite nice. I will show you actually in this exercise that you can actually do this. If it's literally just a single line thing that you're doing, you can just put it on one line without the block, and then it all works nicely. I do that sometimes and it's quite nice. But, yeah.

01:16 There you go. That is, what a an if statement looks like. You can add an else to it to say, if that's not true, then do this. So this is what we call the alternate. So here's your consequence and here's your alternate.

01:31 So if the condition is false, so booleans, again, we're bringing back booleans. They're really important, or at least expressions that evaluate to a boolean value. You can do, chain these together. So you'd say, if this is true, then do, do this. If it's not true, but this is true, then do this.

01:48 If that's not true, then do this. That's what's going on there. And then we're gonna talk about switch statements. So this is the syntax for a switch statement. You're gonna take a value and, on a case by case basis, say if that value is equal to this or that, then, do this or that.

02:04 And if it's not equal to any of those things, then do this. So you basically could take this and rewrite it to be a little bit more like this, in in some cases. So, that's interesting. We also talk about how to add these curly braces and why you might wanna add those. So it's gonna be fun.

02:21 It'll be interesting. And then we get into loops, and this is fun. So this is the syntax for a loop. You've got your initializer, your condition, and what is called your final expression. So at the end of each loop, this is what's evaluated.

02:36 And then to determine whether it should continue looping, this is what it evaluates. And then before you even start, it evaluates this. And every single time, it'll just run this until this condition is, is false. So pretty interesting. You're gonna like that one.

02:52 We do not cover while loops, but, this is something that you run into occasionally, when you don't have a specific number of times you wanna do something or something. You're just gonna loop until a condition is true and then it'll break out. And I'll teach you also how to break out of loops in general. Do while loops are also a thing. We're not gonna be covering that in these exercises, but you might see some do while.

03:15 That's basically the same as a while, except in a while loop, it's gonna check the condition before it loops. In a do while, it does the loop, the the first iteration of the loop and then it checks the condition. So in both cases, it's gonna check the condition on every single run, but in the while loop case, it checks it before it starts running the first iteration. And in the do while case, it checks that condition after it's run that first iteration. So we'll, not go into these specifically, but I wanted to show them to you right here just to give you or to expose you to that idea.

03:47 So when you run into this, you'll know at least a little bit of what to look for and what's going on. We do end up covering a for of loop. We do not cover a for in loop. This allows you to iterate over properties of an object. I don't use this a whole lot, if I'm being honest.

04:04 We'll typically do other things. We'll talk about objects later on. And then the for of loop allows you to iterate over values in what's called an iterable. That's another advanced topic we'll talk about later. And we actually have an example in the exercises where we go over iterators, with an array, which it is a little bit more of an advanced topic that we'll talk about later.

04:27 But I wanted to introduce it to you here when we're talking about control flow because it's I use four of loops quite a lot. And then there's the four await of, which we're gonna talk about that way later with asynchronous programming with TypeScript. But, you can do, basically, it'll it'll run, the iteration with that that first value, and then it will wait until the next value shows up. And that can happen even days later. Like, you don't control, necessarily how when that comes.

04:58 It's just whatever the async iterable decides to send you another value. It's pretty interesting. Again, we'll talk about async stuff way later. And then we get to the ternary operator, one of my favorites, where the whole thing is an expression that is made up of three expressions. That's why it's called a ternary operator.

05:17 And, yeah. It is really nice for a lot of reasons that we'll, talk about. So I'm really excited to go through this one with you. So let's just jump right into it.