Java Programming Tutorial 30 – Variable Scope with Nested Control Flow

hey what's going on everybody this video we're gonna be talking a little bit about variable scope in the context of nested control flow so translating that into English where can we create variables how long do they exist and when are we going to get variable conflicts and so forth so it should be pretty fun hopefully you've picked up on some of this throughout this series but I just want to set it straight make sure we're all on the same page and you know who is on the same page with me at all times that's right our sponsor pram is a free mock interview platform where you can develop your technical interviewing skills practice coding with live execution of all major programming languages to solve real interview questions interview types include data structures and algorithms product management behavioral interviews system design front-end and data science I've personally used this service to successfully crash course for a software engineering interview lots of people are having success getting positions at companies like Amazon Google Twitter and more check it out I'll leave a link for you guys in the description alright so we had an issue in the previous videos where I created this AI variable and down here if i uncomment this and want to do this stuff again we have this error because it says AI is a duplicate local variable so what the crap does that mean well essentially the I we created up here is still in scope when we get down to line 15 and in Java we cannot declare the variable more than once if it's still in scope so we can't go in here we can't create an I've area belen here we're gonna get an error c duplicate local variable i so when we get down to line 15 this one's still in scope so it's gonna be like nah brah you can't do that so when does this variable go out of scope well let's look at the curly braces it's defined in so it opens here the main and it ends all the way down here you use that little box around it so when you click one of the curly braces look around you'll see a little box that is the closing brace assuming all your code is right and it can compile and run so that means we're not able to create another variable with the name I anywhere between these curly braces now if we create a variable in a more nested scope it'll eventually go out of scope and we can reuse that variable again in this situation we could only use the I inside of another method so right now we're in main if we created another method down here so if we made it a custom method here we could use an I variable inside of it because I would no longer be in scope hopefully that makes sense let's go through another simple example with a new variable if we're inside of this while loop right here and we say into pizza set it to the value five that's fine and as school that's okay but we can't go inside this while loop and create a pizza because it's still in scope but outside of these curly braces we can so look at this we can say in pizza and give it the value five and we're not gonna get any errors there that's totally okay as you guys this pizza went out of scope at line fourteen that's just something to keep in mind when you're working with multiple loops inside of the same scope so in this situation we have these nested while loops and then we have this for loop the nested for loops down here we ran into this little issue with this variable so I'm going to do is I'm going to get rid of these pizzas here and we just take a look at this when we create the variables inside of the for loop parentheses like this they are scoped to these curly braces so that is a nice way to keep our scopes clean these variables are not defined in a wider scope than they need to be and that's a general rule you never need to create variables in a scope larger than where they're used so that's the whole issue with the concept of global variables inside of computer science anytime we have a global variable something we can change anywhere that thing is in like the biggest scope possible and it's available to everyone so it's very likely to cause naming conflicts you're likely to either overwrite it or get a compiling error because you're trying to use that name it really depends on the language some languages you're not gonna get that compiling error it's just going to replace it or mask it but anyways I'm rambling the whole point of this thing is just be very clear about where your variables are defined and don't define them in a larger scope than necessary so for example if I went up here and said in equals nine and then for this first iteration all I did was leave that blank this code would still work assuming this wasn't already used up here and that would be fine but then this eye is going to persist down here so I exist down here that was only fine if you need to use the variable I at the end and sometimes that's gonna come up mainly just be really careful about where you're creating your variables and just pay attention and I'm just gonna put that back there we go and again I'm gonna comment this out because we have that naming error and we don't really need these for loops all right cool so hopefully I was helpful guys thank you guys for watching be sure to check out the next video we're gonna be talking about the break and continue key words in the next video or two so those would be really helpful if you want to build more complex applications thank you guys for watching and I'll see you then oh and don't forget to hit that subscribe

Tags: , , , , , , , , , , , , , , , ,

There are no comments yet

Why not be the first

Leave a Reply

Your email address will not be published. Required fields are marked *