 Hey everybody, it's Brian. Welcome to our drum roll. I think this is the fourth. I lost count. Yep, it's the fourth flutter tutorial Today's just been nuts. So forgive me if I'm a little scatterbrained, but We are going to pick up where we left off on control flow statements And we're going to be looking at the while and do along with the break and continue and we're gonna touch on the for loop So if you're reading along in the official documentation, you can see there is this while and do while While loop evaluates the condition before the loop and you can see this nice little example Versus a do while loop evaluates the condition after the loop That's confusing. So let's look at these a little bit here So let's say Let's make a variable let's call it bool Because I like things indented while running we're gonna say print now if you're new to programming one thing you should be aware of is that a Loop needs a break if you will or it needs to be able to Stop execution of that loop or what happens is something like this where it just goes And it just runs forever. You can see down here. It just runs and runs and runs And to kind of illustrate that what we're going to do is we're going to make a variable here and we're gonna say counter If you don't know what that is it's the same thing as the encounter equal counter plus one It's actually a shorthand for that and we're gonna say we'll call this the while counter So if we were to just run this right now as is it's just going to count until eventually against the stack overflow and dies or until The underlying framework resets that into a zero. Sometimes it'll actually flip it back to a zero for you I actually don't know what it does in Dart, but every framework kind of handles that a little bit differently Stack overflow would basically be like the whole program just crashes and you get an error message where it will say Value out of range or something like that So what we really need to be able to do here is say if counter Greater than or equal to 10 actually say five just because i'm lazy Then we will say running equal false So what we're really doing is we're saying hey stop running Um, but we tell it hey set this variable to stop running Let's run that and you can see it says while counter equal blah blah blah down to five So what's going on under the hood here is it's saying while Evaluates this expression must equal true and because we're setting it to false here Every time it reiterates through this loop it's reevaluating. So when it gets to here and that's false it just stops So we can say print Onto the next and you'll see kind of what's going on here And it says bang bang bang bang bang onto the next And then it just stops Now the do loop is very similar And there's our evaluator. We need to reset that actually running equal true Well, let's see counter equal zero And we're going to actually put in some print statement here. Let's not say waiting. Let's say done with while And then down here, we're going to say do loop And we can actually just take this And you'll see that it's pretty much the same thing Probably help if we put this as a do instead of a while so it's less confusing You see it's pretty much the same thing So why do we need two of them? It's actually pretty simple It depends on when you want your statement evaluated. Do you want it before or do you want it after? Typically what this will do is it'll take action Then evaluate the statement. I shouldn't say typically that's exactly what it does. It'll execute the scope of code Then evaluate the statement. So I just screwed that all up And The contrast of that the while statement will actually evaluate and then if it's true, then it'll run it It's a unique difference You can get into some pretty special situations um, you can actually Get into trouble a little bit like if you have uh Like if you're doing like socket programming and you're not ready to receive data yet Or you're not ready to send the data, but you do a do loop instead of a while loop You can get kind of messed up So just understand the the basic differences between that Um, essentially they do the same thing. It's just the order in which your statement is evaluated Now This could also be rewritten Like this What is that actually doing? Um, well, it's doing a few things first. It's ignoring our evaluator and It's actually breaking out of the scope. You have to play that game break out That's what break does. Remember how we said it kind of hits the breaks So it's going to run this scope, but when it hits this break, it's going to say nope I'm done and it's just going to break right out So which one should you actually use? Well, it depends on your situation and what you're trying to do um You could also say Well counter Is less than five I'll actually say less than or equal to five. Let's do this. We're just going to ignore that whole running variable clear buffer Now You can see how things change a little bit here. Now we have up to six Where we really wanted, you know, because it there's this counter in here. We only wanted five now. It's going up to six So you would say, you know less than five less than five Run it again Bang. So you see how it really depends on how you structure your code and how you write it There's really no correct way of doing it. That's the beautiful thing about programming um personally I don't like to put this evaluator here. I like to use a actual variable that way I can check that variable state at any time And then I like to actually set the variable. I don't like to do the breaks Personal preference. That's just how I like to do it. Let me make sure this still runs the way we expect it for the tutorials And now we're going to look at the for loop For loops a little bit different Actually, let's cover continue here Real quick why not We'll say if if counter equal four then Continue This is the difference between break and continue. Um, remember how break will break out of the scope of execution And what we're going to do here is we're going to say I don't like doing that, but we'll do it anyways um Let's run this so you can see exactly what's happening here One two three five. We totally skipped over four So what this is doing is we're saying If and then we're evaluating here The counter is equal to four continue. What continue does is it says, okay? I've heard everything I need to hear. This is a lot like arguing with your wife I've heard everything I need to know and I don't need to hear the rest of this Let's just go back to the beginning. So what this does is it actually says go back to the next iteration And starts completely over Let me go back up here so you can see it. So in this case, it'll go one and it'll go through two It'll go through three. It'll go through four It'll go through hit this and say nope. I've heard everything I need to know and it goes back up here So this never gets executed And on five it runs all the way through this and says hey Counter is greater than or equal to five and we can actually just say Equal to probably a better or streamlined way of doing this The counter is equal five then break, you know, just hit the brakes. I'm done with this whole thing I'm getting a divorce and I'm leaving you and it just jumps out and it says done with the while loop So There you go Now back to what we're going to talk about which is the for loop, which is a little bit interesting For loop if you're new to programming you're looking at this going. Oh my gosh, this looks insane I don't want to type this out. It's actually very simple and it's very easy to get in the first two times You do it you're going to get it down the rest of your life and it's in almost every major language So here we go I equal zero. Let's say I equal zero Notice that little guy right there And then we're going to say I less than five I plus plus So what are we doing here? What is all of this? Let me put this back up here For people on youtube you'll let me and hurt my feelings Let's just print this little guy out so we can see what's going on here And we're saying I zero one two three four. So we've done this five times. Let's let's kind of examine this The four statement actually Takes three separate parameters and I almost wish those were commas, but It's just not how it is and that's a holdover from the old cc plus plus days um, so this first one Is actually setting a variable we're going to say we want a variable named i and set it to the value of whatever In this case is zero Then we're going to say while Because there's actually hidden while in here while i is and then whatever and we're going to say less than five Increment i so there's our incrementer is back here So basically we have what we want when we want to do it and what we want to do to it and then While that whole thing evaluates to true We're going to execute plus we have our variable from the top Confusing but why do we need this? imagine this List and we're going to say string We'll call this list equal new list I have a bit of a sore throat and it's kind of bugging me a little bit here So that's how we make a new Uh list of strings now we're going to say list dot add Everybody always asks me why do you always put your own name in these? I don't know. It's just because i'm on the spot and I just can't think of names. I'll come up with names like Uh slardy bart fast like I did in the previous tutorial Um, I just on the spot can't think of really anything other than like myself and maybe immediate family If I could spell immediate family All right, so let's say we've got a list of people and in this list we've got three people so How would we go about doing this? Remember, this is a zero based index, right? So we're going to say list And we're going to say I is less than length And then we're going to increment. So let's just run this and see what happens here So we know that we have three items first one starts with zero. So what we can actually do here is we can say We're going to try this. I'm not sure if we can do this, but we're going to try it It might blow up and I may have to put it outside, but it'd be really cool if we could do this boom Didn't do it the way I wanted it to do it um Yeah, let's just put it outside That way it's easier for people to follow along So for the sake of the tutorial, this is what we're going to do here We've got our string and in there we have our i variable equals And it's not actually signing. This is just a string and then we're going to catnate. Is that right catnate that string with the value of that item So you can see that zero is brian one is heather two is chris. I kind of want to figure out how to get that to work Element at let's try element at i and see if this actually works here I'm sure there's a way to do this and that's why I'm kind of Wanting to try it here. Forgive me for just a minute See if that actually don't think that's gonna work Fingers crossed Nope didn't do it. All right Ah, dear dart developers if you are our dart, uh Yeah, dart developers would be right if you're watching these videos. Please make that work. That was awesome I'm probably not doing it right. There's probably a better way of doing it, but uh That in a nutshell is how you would do it Personally, I prefer the shorthand Get rid of that scope operator there Same thing so Why do you need the for loop? What's really the difference? I mean couldn't you just say while we are not at the end? Well, you could but it's very ugly so what a For loop replacement would look like We would say Counter equals zero Um, let's say hey while Counter is less than List dot whoops list dot length I have a nasty habit of putting that on the other line counter plus plus as a print And here we're going to say whoops we want it to be counter Actually, let's make it I that way. It's more intelligent here And I I and I And let's run this. Oh, what do we got here? What's going on with this? Invalid value not in range exclusive three. Hmm So what we're saying here is We have it starts at zero And Is less than length? So let's try this. You see why this gets a little hairy here. Let's do this and see if it just runs Okay, so that does run and let's get rid of this guy because that's what's actually causing the error here Let's just see what we got here boom Oh, see one two three. So we got it in the wrong spot. That's really what's going on here So we need to actually increment this afterwards Now it'll run There we go So this kind of uh Illustrates some pretty good point and let's actually put this So we can see the difference between the two Um, so we've got our four and then our four replacement. They do exactly the same thing It's just one is a little bit more complex a little bit more error prone While the other is a little more streamlined and elegant and kind of if you will industry standard So if you ever get like a professional Job writing code and you do something like this Your quality control agent or your manager is probably going to sit you down and say why didn't you use the four loop? Because this is exactly what the four loop is designed for so Anyways, those are the distinct differences between them Just to recap the while loop evaluates before they do while loop evaluates after And the for loop is perfect for going over lists and enumerations Whew, that was a mouthful Thank you for watching if you found this educational entertaining be sure to go out visit my website All the code is out on github and I've got a link out to the repository right now If this was helpful, feel free to donate this site is run 100 off your donations. Thank you