English IDE
<<--- Click this link to write your test program here.
After you have experimented with variables and operators,
click the Yellow Done button to continue.
If it's not a link yet, you might need to refresh your
browser, or else click this button to call a mentor:
Or, if you are like me and prefer to read at your
own pace instead of whatever pace set in some video, You can read
the transcript (and probably come out ahead of the non-readers).
We have six concepts that cover all of computer programming, and you
alread know five of them.
There is only one more concept left to go and you will have everything
you need. Actually, you already had it in the very first program you wrote,
PBJ.
People manipulate stuff -- peanut butter, jam, bread, plates, ideas,
colors, numbers -- you name it, if you have a name for it, you can manipulate
it, and if it's not a physical object, then at least you can manipulate
the name!
Computer programs manipulate information: numbers, names, groups of
names and numbers representing physical objects or just ideas. In other
words, stuff. The information can be very simple, or very complex but it
has to go somewhere, in your hand, on the counter, on a plate, in your
head, somewhere. The computer has millions of places to put numbers and
names representing ideas and things and stuff. We call those places variables,
because what you put there can vary from time to time.
Think of a variable as a container, like a glass of milk or orange juice.
When you pour OJ into the glass from the jug, that's
like Input:
Let's say we have three glasses, one is one quarter full of OJ
(that would be 1), and another is two
quarters (half) full of OJ (that would be 2).
If you pour the first glass into the second (add it to the second),
then it would be three quarters full (3).
Computer variables are like that, when they have a quantity that can be
added, you can add those numbers and get a sum (or subtract or multiply
or divide), except pouring one variable into another doesn't empty the
first, like this:
Steve prefers to think of it like the mailboxes on the walls of the
Post Office or behind the registration desk in a hotel, or in the office
at school. You can put something into the box (at the Post Office only
the mail clerk does that) or take it out. But there are all these boxes,
every one with a unique number, same as the computer. Hardware guys like
Steve like numbers. Programmers don't know the numbers, we just use names
like at the school office. Those boxes have the names of the teachers,
but our computer boxes have names we made up for them.. If you imagine
that there are slips of paper, one in each box, you can take out the paper
and write a number on it and put it back. Later you can take out that slip
and erase or cross out the number and write a different number there. Computer
variables are like that, except you can only copy numbers from some other
box, or do arithmetic (or some logical operation) on the numbers to get
a new number on the slip. My OJ example is more like a real computer, because
you can only add or subtract (pour).
There's a lot more to be said about variables, but it will be easier
to understand when we do a real computer program with real numbers, not
just a sandwich, but a four-function calculator like you can buy for a
couple bucks at the Dollar store or use on your smart phone.
But computer variables are so important, you need to get a deep visceral
understanding of them before we proceed. So let's go back to the IDE
to experiment. Click this button to open it in a new window, which you
can position to one side or above or below the instructions in this page:
Click the Step button again and
the program will do what line 1 tells it to do, then stop with line 2 highlited.
Look at your variables: you see that Avar now has the value 3,
but the others are still empty. This is what the "LET" command
does for you, it takes whatever value is described on the right side of
the equal sign and puts that value into the variable names on the left
of the equal.
Click the Step button again and
the program will do what line 2 tells it to do, which is to put the value
(number) 2 into Bvar.
Stop and think about what you expect to happen before you click the
Step
button again. Then click it and see if you guessed right. It did not put
the whole text "Avar+Bvar" into cvar, nor even "3+2"
but rather it added the values in the two variables and put the sum (5)
into cvar. Click once more to see what the PRINT command
does. In the output region of the IDE it prints out
a sequence of items, the value in Avar (which you can verify is
still 3) then the exact text "+" because it is quoted, then the value in
Bvar
(two) then the exact text "=" followed by the new value in
cvar
(five), all smashed together with no spaces.
You can verify that whatever is inside the quotation marks gets printed
exactly by clicking the red STOP
button (so you can edit the program) then changing just the '+' inside
the quotation marks of the fourth line to " pLus " (with the spaces
and capitalization), then click the green Fast
button and then the usual Run button, so the program runs at full
speed without stopping on each line, and you can see that it prints instead
your replacement text.
What do you think will happen if you put quotation marks around the
"Avar+Bvar" in the third line, like this?
Professional programmers read the reference manual (if it is available)
to learn what the various parts of a program can be expected to do, and
then if they still do not understand, they write little one-off programs
like this to test their understanding.
Today it is trivial and tedious and boring, but another day, when the
documentation is inadequate, you will need to do it, so this is good practice.
We are not done yet. Start over with the original three "LET"
lines, but change the name of the variable on the third line to Avar:
This is a command, not an equation like algebra. It is not saying that
Avar
is inherently equal to Avar plus Bvar (which would be
true only if Bvar is zero), but rather the computer is being commanded
to add the sum of the current contents of Avar plus Bvar
and put the sum back into Avar (which discards its previous value).
Change the plus in the third line to minus (-). Try to guess
what will happen before you click through. Try again with star ('*'
shift-8 on most keyboards) and '/'. Do you believe these symbols
subtract, multiply, and divide?
When you have two or more values together like this with arithmetic
operations between them, it's called an expression. You can also
combine multiple operators in a single expression. In school they teach
you to do the multiplication and division first, then any addition and
subtraction -- unless you use parentheses, and then you do what's inside
the parentheses first, but otherwise the same rules. The computer works
the same way. For example, the first two lines here compute the same value
(11) but the third line computes the value 16.
We have another operator '#' (shift-3) that gets used a lot
and you should know. The English computer you are running this on uses
that symbol to mean "concatenate" that is, to put the values next to each
other as text, but not do any arithmetic, just mash them up against each
other. The English computer assumes the concatenation operator if you leave
it out. Try it. Remember that PRINT line? We didn't type any concatenation
operators, but they were assumed. This is what it really did for you:
Pretty soon we will get to writing a real program, a four-function calculator,
but it uses variables and operators, and you need to understand how these
things work. Be sure to sign in with your Zoom name and do these experiments,
then come back and refresh this page...
Then come back here and click this
Next
link to skip down to the next video.
Video Transcript: 4. Variables
By this time you should have mastered getting your steps in order -- we
call that Sequence -- you can move things around on the screen and
query the date -- that would be Input and Output -- and you can
change what the computer does based on things not known when you wrote
it -- the word is Conditional -- and you can make the computer do
the same thing several times -- Iteration. You also have some experience
doing Top-Down Design using Subroutines.
Experiments
In (speaking) English we really don't have the concept of a variable --
we have containers, boxes or glasses we can put things in or take them
out, but (outside the school office) the idea of a whole bunch of them
with names on them is not the way we think of the world. But it's an essential
way to think about computer programs. So clear out the program panel in
the IDE and type in something like this:
let Avar = 3
let Bvar = 2
let Cvar = Avar+Bvar
print Avar "+" Bvar "=" cvar
Then click the blue "Step" button
at the top right of the IDE. This should open up some
space below the program panel with the label:
English State: Line 1
and the first line of your four-line program is now green. The program
has paused just before executing the first line (Line 1) of your program.
Nothing has happened yet, but it already knows you have three variables
-- scroll up if necessary and you will see the three variables listed with
null values. I gave them the silly names "Avar" and "Bvar"
and "cvar" but you could give them any names you like. It helps
if you use names that represent what purpose they serve in your program,
because when your program gets big, you will forget what you are trying
to do here, and the reminder is good.
let cvar = "Avar+Bvar"
Try it and see. You could also try putting quotation marks around
the numbers on the first two lines and see what happens. What happens if
you only quote those two numbers, but not the sum on the third line?
let Avar = 3
Step through again and look at the
variables after the third line executes.
let Bvar = 2
let Avar = Avar+Bvarlet clean = 5+3*2
let complex = 5+(3*2)
let difft = (5+3)*2
In my examples, sometimes I use numbers, and sometimes (variable)
names. The computer doesn't care, both work. We use numbers when we mean
"exactly this value, not any other," and we use variables when we want
to change the value next time around, or for different inputs. For example,
let one = 1
let two = 5
let some = one+2
let sum = 1+two
let two = two-sum
print "some=" some ", sum=" sum ", two is now " two
You see the name of the variable (in this case "two") has
nothing to do with what value it has. Well, you should pick variable names
that mean something to you (because otherwise you will forget what is going
on), but the computer doesn't care.
print Avar # "+" # Bvar # "=" # cvar
Most computer programming languages will not make these assumptions,
you must explicitly use the operators.