Variables

 Zoom Name:
Video Introduction (00 minutes) <<--- Watch this video first, then

English IDE <<--- Click this link to write your test program here. [Test]

After you have experimented with variables and operators, click the Yellow Done button to continue.

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).
 

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.

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:

If you drink the OJ (or pour it down the sink) from the computer's perspective, that would be Output.

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:

Also, computers don't do orange juice or milk, they do abstract numbers and text. Later you will learn about Objects, which are just collections of numbers and text, plus the programs to work on them.

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:

Don't forget to type in your Zoom name if it's not there already.
 

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.

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?

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?

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:

let Avar = 3
let Bvar = 2
let Avar = Avar+Bvar
Step through again and look at the variables after the third line executes.

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.

let 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.

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:

print Avar # "+" # Bvar # "=" # cvar


Most computer programming languages will not make these assumptions, you must explicitly use the operators.

[Memo: after they have done some of these experiments in the IDE, the following paragraph will be replaced by the next section.]

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...
 

Substring Command

We have a word game coming up that requires the computer to pick individual letters out of a word, or else to poke (usually something else) back in. You can learn how to do this now (keep reading), or you can delay until you get there (skip down to the bottom to start working on the calculator, then come back here to pick up where you left off).

In English the word "word" refers to one or more letters together (with no spaces or punctuation) that has a particular meaning, like I just now explained for the word "word". Computers mostly don't know about meanings, but it's often useful to put letters and punctuations together into a piece of text that gets handled in a single variable. We do this a lot, so we need a single word to describe such a piece of text. It can (and often does) include spaces and punctuations, so the word "word" is misleading. Instead we call it a "string" to be thought of like beads on a real string or thread, like the picture here. So let's assume you have a variable necklace (like the picture) with a string of characters in it:

let necklace = "Hello World"


If we wanted to pick out some of those beads -- say for example the beeds in front, the letters "LLO WO" -- and make a copy on their own string, that would be called a "substring". This is so useful that every language that lets you work with strings at all has some way to extract substrings. In the English computer we do it with a command:

substring part = necklace,2,6


This isn't very much like the English you speak at home, so it needs some explaining. The command "substring" tells the computer what you want it to do.and the next word is a variable to put the result in. The equal symbol tells the computer that it must come from the variable (in this case) necklace. The next two numbers tell the computer how many characters to skip over (two, letters "HE"), and then how many letters to take (six).

The formal reference documentation is here in the Command Details section of the Kitchen Computer Reference Manual., which you can also get to from its link in the Quick Reference section at the bottom of the IDE page.

In the spirit of what I call the "Video Game Method" -- Hmm, I seem to be confronted by this blue orc, I wonder if the Photon Grenade works... Nope, well try the Laser Defragger... Not that either. After nothing I can think of to try works, call in the Mentor for help (or in the more general case, Google the question) and learn it was the Pixie Dust you neglected to pick up on Level 3 -- I offer this little test program for figuring out what the substring command (or any other command) does:

let necklace = "Hello, World!"
substring part = necklace,2,7
print "Got '" # part # "'"


Type it into the IDE, then run it several times and fiddle with the numbers until you see what they do. Or you can use the IDE Step button to step through one line at a time and watch your variables.
 

Six Things Summary

There you have it. Every program anybody ever wrote is composed of commands, each representing one of these six things. You start with the name of what you want to do, and that becomes a subroutine name, the program that does it. You break that down into two or three parts, each with a descriptive name, and each part that is not one of these six things, you make it a subroutine name and repeat.

OK, let's review what we know about the Six Things:

A. A program is a sequence of steps to be executed in order, except

B. If we tell it to repeat (an iteration), it jumps back and does some part of the sequence over and over, or else

C. In a conditional we can tell the computer to omit some step based on some outside or computed condition. In the usual form you get a choice, left or right, red or green, yellow or blue, yes or no, sometimes a choice from several like our choice of jams, but never both or all at the same time.

And what kinds of steps do we do in (or out of) order? We can do

D. Input (so far reading the date, later we will accept information from a human user, like the previous bonus) and output (printing a message on the screen, or changing some pixels in an image), and

E. Evaluating the value of a variable which can then be used to control conditionals and iterations, and/or output to the human watching.

Those five things, every computer program, in its lowest detail, consists of those five things, and nothing else.

F. There is a sixth not-quite-primitive called subroutines, a sort of named box we can put around a bunch of steps so that you think of them as a unit, the way we call the sandwich program "Make PBJ" which says nothing about the steps you went through to make it. You don't need to think about what's inside the box after you know it works correctly.

Our next program is no longer a PBJ sandwich, you will write a real program, a four-function calculator just like the one in your iPhone or you can buy for a couple bucks at the Dollar Store. Later we will rewrite the same program in Java. You will use the skills you learned in the PBJ, and you will use variables.

Turn the page for the Calculator.

[2022 January 8]