PROGRAM WITH VARIABLES AND CONSTANTS

In programming terms, variables are where items of data live. Programs deal with all types of data, such as numbers, dates, names, and letters. Variables provide the convention for naming, storing, and retrieving these different kinds of information. A variable is simply a name used to store and refer to a value. For example, you can define a variable called count and set it to store the number 5 by doing the following:

count = 5

You can then treat this variable the same way you'd treat a number. If you add count to a number, it's just as if you added 5. Another variable, called total , is used in the following:

total = count + 2

where, if count is still storing the value 5, you just set total=7 . You can also do operations such as:

count = count +10

where you just added 10 to the value held in count .

One important point about variables is that, in most languages, you need to define what type of information you're going to use them to store. It could be a type of number, letters, or something else, such as a collection of variables as discussed in the following section. For the previous examples, you need to define count and total before you use them, as shown in the following:

integer: count, total

An integer is a number such as 1, 2, 3, 4, and so on, with no decimal. For other numbers you can use a type called real or float (short for floating point). Different languages store text in different ways; for these pseudocode examples you use a type called string that stores text as a string of letters. The analogy is a string of beads, one after the other.

There are other special variables called constants . These are variables whose values you set once and never change. They can be thought of as special variables. For example, in a mathematical program, you might declare a constant called pi , as shown in the following:

real constant : pi
pi = 3.14

The value of pi is not going to change as you use it, so you store this value in a constant. Different languages use different methods of declaring variables and constants; these are covered later in the course. For now, just use this pseudocode version.

Collections of Variables

Most programming languages also allow you to put your data values into a list or array. A list or array is like a grocery list in real life: It wouldn't make much sense to write each thing you need from the grocery store on separate sheets of paper. Instead, you put all these items into a list.

In most programming languages, you can step through a list of data one item at a time using a control structure such as FOR -- END FOR . Many programming languages also allow you to pick an individual item from a list using an index. For example, if you want to grab the third item in a list, you'd use that list item's index number to access it.

To continue with the earlier e-mail checking example, you might provide the program with an array of names of people who are sending you annoying spam. The inner part of the WHILE control structure would then look something similar to this:

1. Look at the sender of the email
2. FOR (every N in the SPAMMER array)
  1. IF (the sender equals the Nth SPAMMER)
  2. THEN delete the email and stop the loop
3. END FOR

By using an array, you now have a program that can handle multiple spammer names with only one named variable. Arrays combined with the FOR - END FOR control structure make it possible to manipulate large amounts of data with simple commands. In addition, many languages provide built-in functions that work arrays. For example, you can sort an array of strings alphabetically with a single command.

Some programming languages have specialized structures to add additional logic to variables they hold onto. For example, a stack structure holds onto values in the order they are added, and enables you to get at them in terms of their position relative to the top of the stack.