Programming Basics: Part Two Author:
Icydemon
Submitted:
13-Feb-2008 16:22:17
Article Rating: Above Average (# of votes: 3)
Variables, arithmetic, logical operations and operators.
The Variable In programming terms we can refer to a variable as "a fixed amount of space in memory (meaning RAM) where we can store a value needed by a program for its execution". But what the heck is that, exactly? To explain this, let's assume we have a problem wherein we need to calculate the sum of the numbers from 1 to 10. To do this, we need a variable to store the sum of the numbers we add each time. So it looks like this: Sum = 0 Sum = Sum + 1 Sum = Sum + 2 .... Sum = Sum + 10
In our piece of code above, "Sum" is a variable. When we are using a programming language (e.g. C) we have to declare and often also initialize our variable. Each programming language has its own structure and method for declaring variables. In C, for example, you create an integer variable named Sum simply by doing this: int Sum;
Remember also that a variable is only a reference to a position in memory. This means that when we declare the variable Sum in our example, the computer will reserve enough space to hold the contents of the variable, and when we refer to that particular variable, the computer will do the operations with the contents of that particular space in memory. Types of Variables There are three major variable types: numeric variables (which are used to store numbers), alphanumeric variables (which are used to store characters, numbers and symbols in a string format), and logical variables (which store logical values, like true or false. Each programming language has many variations of each of the above types. For example, in C you can declare an integer variable using the int keyword (which can be short or long), you can declare a single precision numeric variable using float, etc. These are all numeric variables, but each one has its own size and is handled differently. For example, the long long double type is 80 bits in length while double is only 32 bits). Declaring a Variable As was said earlier, how a variable is declared depends on the language that you use. In our examples from now on we will use the C style, since most languages inherit C's structure and syntax. To declare in C we do the following: int myVariable;
This declares an integer variable named myVariable. C variables are casesensitive; myVariable, myvariable and mYvArIaBlE are not the same variable. However, C standards for readability dictate that we capitalize the first letter of each word.