Chapter 7 : managing variables


 The Project 

The computer is a robot that processes information, so it is important to specify what this information consists of, how it is stored in the computer and how it is exchanged with users.

 Objectives 

The aim of this chapter is to develop a mental picture of how variables work in order to develop computer programs that manage the variables correctly.

 Introduction 

Computers are robots that are no different from any other robots. What’s special about them is that they’re robots for processing information or robots for managing data.

But what is information or data?

In our case, it can be:

  • - a single character;
  • - a sequence of characters;
  • - an integer;
  • - a real number;
  • - a concept in either TRUE or FALSE form.

That makes 5 types of information.

As we've already said, the robot-computer has amnesia, i.e. it can't remember anything on its own. So it must have a system for storing information, i.e., memory which is divided into cells. These "memory cells" are called variables, because their content evolves and varies as the program progresses.

In the computer and its memory, information is conveyed in the form of electrical currents, and stored in transistor flip-flops. But this doesn't get us very far in our quest to understand how the computer works, or how to control its operation!!!

The breadboards, slates presented in the robots of the previous chapter, gave us an initial mental picture of how information might be stored. But we're going to improve on this analogy to get a mental picture of the variables that can work in many situations, and help us to conceptualize the inner workings of the computer more precisely.

We're going to think of these variables as representing 5 types of breadboard. Each one can have a label to identify it and depending on its width, can also have stickers whose size corresponds to the information type to be stored on it. A hole in the breadboard allows it to be hung on the "wall where the computer operates". Here's how it looks:

The different types of variables The free stickers

The computer has a great number of free stickers of all sizes. On these stickers, the computer can write various information corresponding to their size, and then put them on the appropriate breadboards as identified by the labels.

When a sticker is stuck on a breadboard, the information that was there before can no longer be found – it is lost.

From now on, we'll call these breadboards variables, and we'll be able to declare as many of them as we need, in sizes approporiate to the information we want to write on them, and we’ll identify them with a name written on each label.

Transfer from one variable to another.

The computer can copy the information on a sticker from one variable to a free sticker and then put this free sticker on another variable. The information on the variable that was copied (the source) remains unchanged. The old information on the destination variable (the target) is definitively lost.

Transfer from one variable to another

However, care must be taken when copying content into (assigning to) a variable. The type of information copied must be compatible with the type of the target variable.

If you change the type, what you can do: put a "smaller" into a "larger"...

  • Assign a string variable with a character;
  • Assign a real variable with an integer.

If you change type, what you can't do: put a "larger" into a "smaller"...

  • - Assign a character variable with a string;
  • - Assign an integer variable with a real;
  • - Change from numbers to characters or vice versa without appropriate processing;
  • - Booleans must be kept to themselves; transfers from or to other types are not possible.

Meaning of the = sign

In the example above, we have given the instruction Copy content of AMOUNT to SUM, which asks you to assign SUM with the content of AMOUNT. In coding language, this instruction would be written SUM = AMOUNT, which could lead to ambiguity. In this case, the = does not represent a comparison, but an assignment instruction asking to equalize the content of SUM (on the left side of the =) with the content of AMOUNT (on the right side of the =).

To avoid any ambiguity in the chapter on pseudo-code, the assignment sign used will be Le signe d'affectation

We can therefore write SUM Le signe d'affectation 25 + 12, so that the value 37 is assigned to SUM.

To summarize

  1. All variables must be identified by a unique name;
  2. A variable can only be read after it has been assigned at least once;
  3. Assigning a variable permanently destroys its old content;
  4. Reading a variable does not modify its content;
  5. When assigning a variable, the type of the assigned content must be compatible with the type of the destination variable.

Let's see how well you've mastered all these concepts by doing a few exercises on variables.

Good work.

Return