Hey, guys, what's up?
So today we are going to study a new topic called Scope of Lady of.
So, for example, if my code is in DeMain.
Suppose I have a variable and I.
And suppose I have a for loop.
And I'm doing some work here, and for example, if I create a variable G inside the for loop.
OK.
And then I am printing the value of G outside the for loop.
So my question is.
Is this correct?
So is this code correct or not?
So what is happening here?
You declared the variable G inside the for loop and you are trying to access the G outside the for loop.
So Compiler will give us added.
And what error compiler will give compiler will say, I don't know, Compiler will say, I don't know
what DG.
I don't know where this G.
Why?
Because the existence of G is between these brackets, these curly brackets.
OK.
You have declared a variable J inside the for loop.
And since this J is declared inside the for loop, so its existence is between these calibrate codes.
Only if you are trying to access if you will try to access this J outside this Becket's then compiler
will say, hey, I don't know what this G.
OK.
So if someone ask you what is scope of G, so what is the scope of variable G?
So the scope of J.
Is between these curly brackets.
Outside these Carlebach kids.
Compiler don't know where d.g.
OK.
Now, let's see.
Okay, so let us write some code to get a better understanding.
So let's name it Scope Nazi, BP.
So I will show you some exciting things.
Suppose I have a variable index equals 10.
OK.
Now if you will create the same variable and X, then compiler will show you edit.
Let's see.
So the compiler is showing us error and error is that you can't have two variables with the same name.
So X equals 10.
And you are again making a variable and X.
So compiler will say, hey, buddy, you can't have two variables with the same name.
But let me show you.
Some interesting things.
So what I will do for and I equals zero I less than two five.
I bless, bless.
And let's see here I am making a variable again with the same name X.
Let's say your index equals I.
And then I am printing the value of X.
And Len.
OK.
So what do you think Combiner will show us at it or not?
OK, let's do one more thing.
Let us spend the value of X, also see outtakes.
Let's see what will be our output.
So my first question is whether compiler will show us error at line number 10 because we are creating
a variable X.
And the name already exists.
We are already having the same variable variable with the same name.
So let's see.
No, the compiler is not giving us added way.
Let's try to understand why the compiler is not giving this error in the previous case.
Kambala showed us at a.
So just try to focus.
What was the last case in Texaco's 10 and thanks so come by, Lessie.
Hey, you can't have two variables with the same name.
I say, OK, now here I am again, having variables with the same name.
Then Weida come by Lisen or jingoes error because they have different scope.
The scope of this variable is between this Blackard and this Blackard, whereas the scope of this variable
X is between this Blackard and this Blackard.
OK, so the main reason is different.
Schoop.
They're too variable, X has different scope.
OK, so these two variables X are different.
And the main reason is because of the different scope.
OK, so this variable.
Let me clear all this.
So this variable X is different.
This variable X is different.
And what is the reason?
Because they have different scope.
The last example was and X equals 10 and X.
They both were not different because their scope was him.
OK.
Their scope was same.
So come by level.
Show us at it.
But here, their scope are different.
So they will be treated as different variables.
OK.
So they will be treated as different variables.
One more thing that I want to tell you is.
Let's see the output.
Where does my output?
So now I put this coming out with this.
Now let's see how it comes.
So first of all, I told you, this is different takes.
So what I'm doing are variable, I is varying from zero to five and the value of I assigned to X and
then I am printing X.
So zero to four is expected.
Zero to four.
Will remain output.
This is the output of loop.
OK.
So what is the output of loop output of Lopez?
Zero.
One, two, three and four.
Now, focus on line number 14.
So the value of X is ten.
If we treat this X and the value of X is four.
If I treat this X, so vich values getting printed, then why not four?
Because this variable X doesn't exist after these Beckert.
OK, so this X is this X?
OK.
Just one more time.
First thing first, the sex and the sex are different.
OK?
What is the output of the loop, aka the loop is zero.
One, two, three and four.
Now, with each line number 14.
So red line number four.
Ding!
I am bringing the value of X.
OK, so which X is this.
This one or this one.
So obviously this one to be Y because this X exists only between this and this.
So obviously this X is this one.
Now what div.
Now, what if I remove and from here?
What will happen to this is normal conditions.
Let's see.
So I would argue this is zero, one, two, three, four.
And again, four in the last example, it was 10.
OK.
Now let's see what is happening here.
So there is no confusion in this.
So there is no confusion.
Why?
Because there is only one X and Y.
Can I use this X between this X?
Because the scope of this X is between this and this and full loop comes between this and this.
OK, so I can use X inside the loop so X equals I and bring the value of X so zero.
One, two, three, four.
Of this, now I am bringing eggs.
So this eggs.
Should be good, Brender.
But this X and this X is same.
If I am changing the value of X, this X value will also change.
Basically the same.
They are same.
So since they are saying my output will be for.
Because the law desperately affects this food.
One more time.
And this woman, this ex and this ex Assim.
OK.
I am changing the value of X.
X equals I.
So this value will be changed.
And finally, the updated value, which is full, will get printed.
OK, now one more thing.
What will happen if I try and print you out?
Let's say I.
So I think Compiler will show us a way, because this eye is a part of for loop and hence the scope
of variable eye is between this Blackard and this record.
And I am using it outside the for loop.
So Compiler should source it.
And I should all cut.
OK.
So there it is.
That is very simple, Combiner don't know what is a.
OK, now let's see some more interesting things.
So you have to tell me what will be my output?
So InterMune.
And it's suppose now there's a for loop.
There's something written, and now I'm printing the value of X.
So what will be my output?
OK.
So tell me where to live, my output.
So the answer is very simple.
Output will be a Garveys value.
Sodrel will be at that rate.
When do I have not given X anyway, Lou?
So when you will train for index, you will get a Gadsby's value.
This question is not at all related to scope.
I just give this cushion to open your mind.
OK.
So what value output are garbage value.
Now you have to tell me if I try and print.
Let's say.
And I mean.
And eye for and eye equals zero, eye less than, let's say, five.
I bless, bless.
I am doing some work here and now I am finding the value of a.
So widely we might output.
So you have to tell me whether there will be another compilation error will occur or otherwise.
Tell me what will be Moudud?
OK, so I think compilation that I will not be there.
Why?
Because this I and this I, they both are different.
Why?
They are different?
Because they have different scope.
The scope of this eye is this record and this record.
The scope of this eye is this record and this record, since these two eyes are different.
They are independent.
Now, add this line, I am bringing the value of I.
So at this point of time, only, only when I exist, only when I exist.
This I.
Does not exist.
This I.
Does not exist outside records of loop.
OK.
So this I.
Doesn't exist at this line.
OK, so add this line.
I have only one eye.
Which is this one.
Since I have only Varnay, I will print the value of the day and hence my would probably be a God visually.
OK.
So if you don't believe me, I can show you.
So what I will do here, I just have to create two variables, and I know at this point of time only
when I exist in the memory.
Now, when I did this.
Now they exist to in the memory.
But those two, why they are different, why they're different because they have different scope.
Okay.
Now, I am not doing anything inside the loop.
And here I am bringing the value of a.
Now, again, headline number 23, there exist only one eye in the memory, and this is that I had somehow
butchered we are garbage value simple.
So the output is our garbage value.
This was also the same case.
Now, let's go into doubt.
So when here I was having an thanks.
So what was happening here?
At this point of time, line number seven, there exist only 16 in the memory lane, number 10.
They exist to extend their memory, but they both are different.
Why different?
Because different scope now at lane number 14.
I am bringing the X at lane number 14.
They exist only when X and the memory Y because this X as soon as this X will cross it boundary.
It will not exist, okay?
It will die or it will not exist.
So headline number fording.
They exist on live next in their memory, which is this one.
But as soon as you reach line number 16, you are printing them well, if I come by, I will say I don't
know I.
Why you don't know?
Because I exist.
Well, the for loop, as well as the for loop end, and it doesn't exist in the memory.
OK.
So I hope you've got some feel of what is its global variable.
So what we will do now and next we do I will cover what is global variable.
And then next we do I will cover some more questions on scrollbar.
Very well.
And global variable mix.
OK.
So this is it for this video, if you have any doubt, you can ask me, OK?
Thank you.