Home
Introduction
Courses
Glossary
About

Beginner - Variables And Constants

Perhaps the most fundamental concept in programming is the concept of variables and constants. You probably know that you'll need to deal with them a lot, so here's a refresher. (To learn about how to implement these concepts in Pascal, go to the course "Implementation In Pascal".)

What are Variables and Constants?

To answer that question, you'll first need to define a variable. Well, have you ever learnt Algebra before? It's the same concept. Just take a name, and assign it a value. It's that simple. For instance, you could assign a a value of 2. So, a plus a is the same as 2 plus 2, which equals 4. Easy does it.

Well, then you may ask, "If it's so simple, why call them variables, and not something else?" Ahh, good question! You see, the name comes from the very nature of variables in programming. They can vary. Sure, I know it wasn't like that in Algebra class, but our aim now is not to find out what x is, but to instead use x as just a storing place for a value. Get the idea? Our x is just a storing place for a value.

Then what about constants? Well, constants can be said to be the exact opposite of variables. Instead of just being storage space, a constant replaces values. For example, if we had replaced every occurrence of the word "lamb" with the word "head" in a certain famous nursery rhyme, we would have got:

"Mary had a little head,
its fleece was white as snow.
And everywhere that Mary went,
her head was sure to go."

Yes, it's a very strange version of the original classic, but that's only if you had read it literally. If I had said that "head" was just a constant representing "lamb", it would have made perfect sense. But what are constants for then? Well, constants were created to make program code more readable by replacing meaningless values with a more meaningful name. A very nifty feature indeed!

Some more examples of Variables and Constants

Still can't get the idea? Don't worry, here are a few more examples to help you out. (Actually, they're more of analogies than examples...)

  • Analogy for Variables

    <IMG SRC="media/b0201.JPG" WIDTH=290 HEIGHT=140 ALIGN="RIGHT" ALT="Variables are like Boxes">

    One method we can use for explaining a variable is to use the analogy of a box. Imagine that a variable is actually a box carrying the value which it stores. Now, you can actually take the value out, and put in a new one. After that, you can look up the value in the box and do things with it. You could also copy the value into other boxes, or take the values from other boxes and put them in your box.

    However, in most programming languages, you would need to declare variables before you use them. Why is that so? Well, using the same analogy as above, declaring variables would be like making those boxes for you to store values in. (You don't expect empty boxes to be lying around, don't you?)

    But here's where the concept of data types come in. You see, just like in real life, boxes come in all shapes and sizes. So, some boxes can't store certain values beyond their limits. Ahh, you guessed there was a catch! We'll introduce Pascal's data types later in the course "Implementation In Pascal".

  • Analogy for Constants

    Constants are like tables

    While variables can be compared to being boxes, constants can be compared to being a look-up table, or something you'll use to look up unknown terms. When you come across a constant, you would have to look up this table and understand what it means. For instance, if you declare a constant 'Name' representing "Tan Chun Ghee", the sentence "I think Name is really great!" would translate to "I think Tan Chun Ghee is really great!" This would be the same with the sentence "Name is my idol!" which would translate to "Tan Chun Ghee is my idol!" As you can see, "Name" is explicitly replaced with the text that it represents.

Conclusion

Now that's we've got variables and constants, you might want to go on to "Control Flow Structures", where another basic concept is revised. Remember though, you don't have to go through these courses in the order they are presented. We have (quite painstakingly) tried to make them independent on their own, so feel free to explore!

PreviousNextTop