Constants and Variables....
So, you want to make a computer do something useful.
Well, then data is going to be involved one way or another.
Even the simplest programs (like 'Hello World') deal with data! Some of it might be invisible to you - but it is there.
Let's take a look at Hello World. First of all, if you don't already know - the objective of this 'first' program whenever you are beginning to program in a new language is to print out 'Hello World!' (or some variation of that).
We may as well take a look at the objective to figure out what the algorithm and pseudo-code would be for this.
First the algorithm:
Most programming languages automatically open a connection to the screen for you so this step is almost always ignored. But if you think about the fact that it is happening (even if behind the scenes) then it will make your life easier on that day when you need to get input or send output somewhere that is not automatically opened for you.
Right now, we're going to ignore steps one and three since most languages do manage your basic input and output devices for you.
So here is the pseudo-code:
The data is the text 'Hello World!'.
Well, this is the simplest version of the Hello World program. All you have to do is print out some piece of text that you already know before the program ever even runs. Let's make a small change that will be a first step toward making this a little more interesting. We'll store the text that we want to print in a constant since it is not going to change.
Here is the updated version of the pseudo-code:
We're up to two steps - that will take more actual code to do exactly the same thing as before!
And that is true. It is more code for you to write. But as far as the computer is concerned it is probably exactly (yes exactly) the same thing. Also, what if your program was much larger and you wanted to print the same message at multiple places in your program? Some day, someone might want the message to be different. And if they did - the old version of the program would require you to search through and find every place that you were printing out the message to change it. Using a constant would mean that you only needed to change one place. In that case - much better for you! This simple case really would be silly to use a constant though.
May as well not stop there though. What if we want to ask the user for a name to say 'hello' to?
New pseudo-code (including the 'automatic' steps that the computer is doing for you):
Well, then data is going to be involved one way or another.
Even the simplest programs (like 'Hello World') deal with data! Some of it might be invisible to you - but it is there.
Let's take a look at Hello World. First of all, if you don't already know - the objective of this 'first' program whenever you are beginning to program in a new language is to print out 'Hello World!' (or some variation of that).
We may as well take a look at the objective to figure out what the algorithm and pseudo-code would be for this.
First the algorithm:
- Open a connection to some output device (usually the screen)
- Print the string 'Hello World!'
- Close the connection
Most programming languages automatically open a connection to the screen for you so this step is almost always ignored. But if you think about the fact that it is happening (even if behind the scenes) then it will make your life easier on that day when you need to get input or send output somewhere that is not automatically opened for you.
Right now, we're going to ignore steps one and three since most languages do manage your basic input and output devices for you.
So here is the pseudo-code:
- Print 'Hello World!'
You might be thinking:
Where is the data? I thought you said it took data to do anything useful!I did say that. And there is data hiding in plain sight!
The data is the text 'Hello World!'.
Well, this is the simplest version of the Hello World program. All you have to do is print out some piece of text that you already know before the program ever even runs. Let's make a small change that will be a first step toward making this a little more interesting. We'll store the text that we want to print in a constant since it is not going to change.
Here is the updated version of the pseudo-code:
- Store 'Hello World!' in a constant
- Print the value of the constant
We're up to two steps - that will take more actual code to do exactly the same thing as before!
And that is true. It is more code for you to write. But as far as the computer is concerned it is probably exactly (yes exactly) the same thing. Also, what if your program was much larger and you wanted to print the same message at multiple places in your program? Some day, someone might want the message to be different. And if they did - the old version of the program would require you to search through and find every place that you were printing out the message to change it. Using a constant would mean that you only needed to change one place. In that case - much better for you! This simple case really would be silly to use a constant though.
May as well not stop there though. What if we want to ask the user for a name to say 'hello' to?
New pseudo-code (including the 'automatic' steps that the computer is doing for you):
- [automatic] Open a connection to an input device
- [automatic] Open a connection to an output device
- Ask the user for a name and store it in a variable
- Print out 'Hello ' plus the value of the variable (and an exclamation mark too)
- [automatic] Close the connections to the input and output devices
The reason that I added back those automatic steps is that the computer is holding a reference to the connections in variables too! The makers of programming languages do all of that work so you don't have to worry about it (usually). And, they usually will make convenient methods to either read input from the user or send output back to them that will always be available to you.
To summarize, all data that is available to computer programs is either stored in constants or in variables. If the data will never be changed - then it is best to store it as a constant. If the data either might or definitely will be changed - then it should be stored in a variable.
I've been trying very hard to avoid any particular programming language because there are subtle or significant differences in how each one deals with them. For example, Python and JavaScript do not require you to tell the program what type of data will be stored in a constant or variable - it will figure it out once you assign a value. Java requires you to tell the program what type of data will be in each one when writing the program. There are advantages and disadvantages to both. When you pick a language, you will get to learn what those are.
Next time we'll take a look at control structures...
Comments
Post a Comment