G+_Nevrin O Posted March 30, 2014 Share Posted March 30, 2014 Its funny how hard it is to simply explain what a variable is, especially because it is so vital to programming among many other things. So im going to make an attempt at explaining it in a way that I hope you can use to explain to others. First a variable is used to store some type of data usually something that might change depending on what its purpose is (it is a variable because the data in it can vary). A variable has two parts, A"Name" which is used to identify the variable in the code, and a "value" which is what is stored in the variable. I have heard of many analogies for variables but the one i think works the best is "a variable is like a box or container with a label on it. The name tells you which box is which and the value is what is inside the box." The type of variable usually falls within one of two catagories (depending on the language). The first is primitives. These types of variables are your int, double, float, bool, ect. These are common and are pretty much universal across most languages and because of that they tend to be easier to interact with. The other catagory is objects. objects can hold just about anything you put in them you can even put in other objects, sort of like a box inside a box inside a box. im going to avoid objects for right now and stick with more basic stuff. Ok, so the value inside a variable can change but the name cannot. think of it like writing on a box with perminent marker. You can move that data/value to another box but you can't unwrite the name. so lets say we have an integer variable and lets call it myVariable and while you don't have to I'm going to give it a value when it is initialized. int myVariable = 7; so the first part (the "int") is me declaring that my variable/box can only hold integers and nothing else, And it can only hold one integer because it is not an array (which I might post about later if people enjoy my rantings). Next is the "name" of my variable/box this is so that when ever i later call this variable in my code if i entered that name exactly it will go "oh you mean this piece of data in that particular section of memory you had me set aside earlier". the "=" means i want to stick whatever is on the right side of this symbol into the variable/box named on the left. And the 7 is just an integer. So now that i have this variable declared i can now use it in my code. if(myVariable == 7) { myVariable = 13; } else { myVariable = 42; } so all this is doing is changing the "value" inside myVariable. and as you can see i can use this variable multiple times and in different locations in my code. You can also set a variable to another variable (in the case of objects) or the value inside another variable. Say we have 3 variables named x, y, and z. and lets make them all ints for now. int x = 7; int y = 14; int z = 4; we can interact with these in many different ways. If we did say try and add x and z together like so: Console.ReadLine(x+z); will output: "11" not "x+z" or "xz" so its the same as writing 7+4. we can also replace the value in the variable or change it like: x = y (this will make x value 14 also note that this doesn't change y so y is still 14) x = y - z (this will make x value 10 it will overwrite the 14 that is there from the last line and replace it with y value 14 minus z value 4) x = x + y (this will replace the x value of 10 with the current value of x which is still 10 because it hasent been replaced yet + the value of y which still is 14 leaving us with 24) x += y (this means the same as the previous line basically take x and add y to it) x++ (this means take the value of x and add one to it and put that back in x) x-- (same as before but minus one) while it may be hard to see a use with these examples for using a variable instead of the number its replacing just try and think of situations where the number changes depending on what needs to be done, a counter is a simple example. there is a lot more to what a variable is and can do and i seem to have distracted myself and forgot what else i wanted to put in here but i do have one last point i want to make that i see get wrong all the time and it is a big difference between primitive variables and objects. first I'm going to create an object that does the same thing as a primitive class myObject { public Int Value; } then I'm going to make 2 objects and 2 primitives myObject a = new myObject(); myObject b = new myObject(); a.value = 5; b.value = 7; int x = 4; int y = 9; now all 4 of these variables serve basically the same purpose they all save 1 integer. so now if you try and replace one value with another x = y (the value of x is now 9 which works perfectly) a = b (the value of a is now 7 so it works perfectly too right?) well not exactly in the first example y's value was copied over to x but in the second the pointer that was pointing to where a is located in memory switched and is now pointing at b so when we try and change the values of y and b: y = 3; b.value = 1; our 4 variables' values look like this now x's value is 9 y's value is 3 b.value is 1 a.value is 1 as you can see it changed both a and b and that is because they are both pointing to the exact same object. also if we declare them like this: myObject a = new myObject(); myObject b = new myObject(); a.value = 5; b.value = 5; int x = 9; int y = 9; if you need to compare 2 variables you can do this (y == x) and it will compare the value of the two and return true but if you compare (a == b) it will return false because they are not the same object even though they have the same value. instead compare the values like (a.value == b.value) though i was always told the proper way of doing this compare is (a.value.Equals(b.value)). Thank You all reading hope your eyes are not bleeding too much. Please leave a comment if you have questions or just want to tell me I went into too much detail on the simple stuff and glossed over the complicated parts (I do that a lot). Also this post was sparked by Joshua Essary's post for teaching variables. I couldn't find much to recommend for activities etc so i hope this at least helps you to explain it to others. pictures might help too or pen and paper :) Link to comment Share on other sites More sharing options...
G+_Dean T Posted March 30, 2014 Share Posted March 30, 2014 I just self describe variables as data which can vary. I think its as simple as its name. A value of data which can change/vary. Link to comment Share on other sites More sharing options...
Recommended Posts