Variables | Difference Between Declaring Variables Before Or In Loop.

In this Tutorial, we will Differentiate Between Declaring Variables Before Or In a Loop, It is the same as local and global variables where the variable is declared outside the for loop as a global variable and the variable is declared inside for loop.

Variables

Variables

Variables are the places where we actually store our data to use that data in the execution of the program later, that is what we call storing our data in the form of variables.

There are two types of variables For outside the loop we call the global variable and inside the loop, we call local variables which have the range to be used only inside the loop.

Variables Inside Loop And Outside

Let’s understand it through some examples where we can see the range of access till where we can access the value in the whole program and could specify the limit.

int myFuntion(){
   var a=12
   var array=[12,34,56,6]

   //statements
   for(int i=0;i<array.length;i++){
      System.out.print( array[i]*a);
   }
}

Here we can see how the variables declared outside for loop can be accessed even inside for loop and out of the loop, both places without any restrictions whereas the variable declared inside for loop could have a range only inside the for loop where we can use it.

And outside the loop, it does not have anything to do with and we can redeclare the same variable outside the loop which is unique for local variables.

That is why it is recommended to declare all the important variables globally means outside the loop so that we can use the variable inside as well as outside the loop.

Learn more about global and local variables at Variables inside the loop and outside the loop.

And also must visit Java Tutorials By Prakhar Yadav to learn more about the solutions of java and get a better understanding of java.

And look at How Can I Read Inputs As A Numbers? In Python for learning more about python programming and other programming languages.

Leave a Comment

%d bloggers like this: