CyberArmy University | Open Source Institute | CyberArmy Intelligence & Security | CyberArmy Services & Projects

[Library Index]

[View category: Programming] [Discuss Article]

Programming Basics: Part Two

Article Rating: Above Average (# of votes: 3)
Author:      Icydemon
Submitted:      13-Feb-2008 16:22:17
 


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 case-sensitive; myVariable, myvariable and mYvArIaBlE are not the same variable. However, C standards for readability dictate that we capitalize the first letter of each word.
int MyVariable;     //an Integer Variable (numeric)
char MyCharacter;   //a Character Variable (single character, alphanumeric)
bool MyLogic;       //a Logical Variable (true or false)        
Initialising a Variable
We said that a variable is some space in memory. Sometimes, our programs occupy a place in memory that might also be used by another program and might also store the "garbage" of that other program. This means that our declared variable (MyVariable) might contain data from another program. So, our MyVariable might contain something like 3483 when we want it to contain 0. To ensure that the variable has the data we want in it, we have to initialise the variable. This is usually done in the declaration statement, but it can also be done later.
int MyVariable = 0; //Declare and initialise the variable
-- or --
int MyVariable; //Declare the variable
MyVariable = 0; //Initialise the variable
Now that you know how to declare and initialise variables, let's get into some interesting stuff that is also fundamental to programming, namely using variables.

Operations & Operators

One of the most important things in programming is operations, mainly logical, comparative and arithmetic. An operation can be generally defined as:

Something [equals] something [operation] something else
(e.g. x=3+2).

he basic arithmetic operations (and how you wrote them) are addition (+), subtraction (-), multiplication (*), division (/), raising to a power (^):
//Arithmetic Operations and Operators
int a;
int b;
int c;

c=a+b; //Addition
c=a-b; //Subtraction
c=a*b; //Multiplication

c=a/b; //Division as long as B is not equal to 0!

c=a^b; //Raises a to the power of b
There are also two other operators: Div and Mod. Div returns the integer portion of a division operation, and Mod returns the remainder. In normal division, if you say s=5/2, s becomes 2,5.
If you Div the same numbers, (s = 5 div 2), then the computer performs 5 divided by 2, which equals 2 with a remainder of 1, and in this case, s becomes 2.

On some occasions you will want to know if a division leaves a remainder (or you will need that remainder). In this case you use the Mod operator. Using the same numbers as the previous example, s=5 mod 2 will return 1.

Note that you can do many opoerations at once, and also mix immediates (numbers) and variables, e.g.:
 MyVariable = 3*(2*x)^32+1;
Programming math works just like regular math in terms of operation precedence. The quick acronym for this is PEMDAS: parentheses, exponents, multiplication, division, addition, and subtraction, in that order. There are plenty of Internet resources on this if you need to learn more, so I will not go into this further in this article.

Comparison Operators
Comparison operators are used for comparisons, and more generally, in control structures (i.e. when we want to see which is the greater of two things, or if two things are equal.) We have 6 operators we can use for comparisons:
<    less than
<=   less than or equal
==   equal
>=   greater than or equal
>    greater than
!=   not equal (also as <>)
We will see these later, so I will discuss them then.

Logical Operators
This is maybe the hardest part for someone not familiar with computers to understand, because it requires you to think in a different numeric system.

People use the decimal system - we count from zero to nine, add 1 to the decade and start again. Computers can only think in binary. Binary is a numeric system composed of two numbers, 0 and 1. It is also called binary logic, true or false, good or evil, and so forth. This is because computers can only know if there is electrical current or not (thus 0 and 1), and they don't have 10 fingers to count with. But this electrical current helps in calculations.

Now assume you have not 10 but 2 fingers. How would you count?
Like this:

0 = 0
1 = 1

We are out of fingers, so add 1 to decade and start again:

10 = 2
11 = 3

We are out of fingers again, what do we do? Add 1 to decade. We are out of fingers there too!

100 = 4
101 = 5
110 = 6
111 = 7
1000 = 8
And so on to infinity...

So, computer can do logical operations on top of that (operations we everyday do without even knowing that we do them). So we have:

Logical Addition (OR):
If Something is True or SomethingElse is True then the result is True
If Something is False or SomethingElse is True then the result is True
If Something is True or SomethingElse is False then the result is True
If Something is False or SomethingElse is False then the result is False

Or in binary:
0 OR 0 = 0
1 OR 0 = 1
0 OR 1 = 1
1 OR 1 = 1

We have Logical Multiplication (AND):
I think you get it now, so I am only going to show you the binary table of AND:
0 AND 0 = 0
1 AND 0 = 0
0 AND 1 = 0
1 AND 1 = 1

We have NOT, which simply swaps 0 and 1:

NOT 0 = 1
NOT 1 = 0

and eXclusive OR (XOR):

0 XOR 0 = 0
1 XOR 0 = 1
0 XOR 1 = 1
1 XOR 1 = 0

That's what happens in the computer. In reality, replace 0 with false and 1 with true, and we can get something logical enough for us. These operators are used in logical checking and in control structures, which we will analyze at a later time.

Conclusion
Today we learned about variables:
  • A variable contains data stored in a particular space in a computer's memory
  • A variable can be numeric, alphanumeric, or logical
  • A variable's name must begin with a letter and can contain numbers, letters and underscores (_)
  • Variables need to be declared and sometimes initialized
We also learned about operators: how to use them and what categories are out there, and we iwll be using these in the future. We also talked about the binary system.

In the next article, we will continue with control structures (logical checks, as well as repetitions).

This article was originally published by CyberArmy.net in the CyberArmy Library.

You must be logged in to vote on an article

About Us | Privacy Policy | Mission Statement | Help