Why when we write INT X inside the FOR LOOP it gets DESTROYED when we EXIT the LOOP and after that if we use cout<<x<<endl; it gives the values of X which was ASSIGNED BEFORE THE FOR LOOP as shown in the VIDEO.
Also please explain the difference when inside the for loop we write just X=0 and when we use INT X=0. Also please explain the heirarchy in which the OUTPUT PRINTS X
Why when we write INT X inside the FOR LOOP it gets DESTROYED when we EXIT the LOOP and after that if we OUTPUT X it gives the values of X which was ASSIGNED BEFORE THE FOR LOOP .Also diff. when inside the FOR LOOP we use X=0 and when I use INT X=0?
int x is a local variable as it is defined inside main()
so it’s scope is inside main() only
the variable x which is define inside for loop () has scope inside the curly braces of for loop
outside that curly braces it doesn’t exist
outside for loop only one x exist which is define before loop
A scope is a region of the program and broadly speaking there are three places, where variables can be declared −
- Inside a function or a block which is called local variables,
- In the definition of function parameters which is called formal parameters.
- Outside of all functions which is called global variables.
Please also answer the second part of my doubt difference when inside for loop I write X=0 AND INT X=0
i have also replied to this doubt as well
I did not understand can you please explain again ??The second part of the question that is.
exactly which thing you did not understand??