Tuesday 27 November 2012

What are Variables and Constants?

So far I haven't written anything about a very important concept. Every program can be divided into two things:

DATA


Data could be thought of as statements about the world. For example, my name is Ray. Or John. Or this box has 12 apples. In procedural programming there are two types of data:

Data that changes - which are often called variables
Data that stays the same - which are often called constants.

We've met data already. In the second program we declared a variable like this:


        // declares three string variables
        string strFirstInitial;

We can then change the variable strFirstInitial in the code, for example like this:

        cin >> strFirstInitial;

In the above code a function reads in a string from the keyboard then stores it in strFirstInitial.

We've also met constants in the first program which we declared like this:


        const string cstStrMyName = "Ray";

The thing to remember is that variables can change. Constants can't.

PROCESS


Process is what we do to the data. For example, above we read in information into strFirstInitial. You can also do mathematical operations (to change the data) and also control statements (such as if) which affect what happens in the program.

There you go: programming is essentially the art of processing data correctly. Where correctly means "the program should do what you want it to do to the data."

No comments:

Post a Comment