The first thing we will in Webol is how to tell the computer to do
something right now. This is called an immediate mode
commands. One such command, which we will learn very soon, is
the PRINT command which tells the computer to write
something.
But, first, we need to learn about the Webol console. You should open the console now, in a different window or tab, by going to the Webol web page.
This page includes several sections, most of which will only be important later on.
For now, look for the section that says
"Cmd:". We call this part of the screen the input
line. This is where you will type commands to the computer.
The big box on top of this is called the console. It is where the computer will display its answers.
PRINT CommandThe first command we will learn is the PRINT
command. This tells the computer to "say" something. We can tell it to
print a number, or a message. We can even solve arithmetic.
To give a command, you need to type it, followed by
the enter key. Each time you do this, the computer will
repeat your command in the console, and then show its answer.
Try the following:
PRINT "Hello"
PRINT 3
PRINT 3+7
PRINT "3+7"
Question: What did the computer answer to each of these commands? What was different about the last two commands? What do you think the quotation marks mean in the print commmand?
Question: what do you think will happen if type PRINT hello to the computer? Try it now.
In immediate mode, the computer does exactly what you ask, and does it immediately. This is very nice if you want to use the computer as a calculator, but sometimes you want to do more: You want to give the computer a set of instructions for it to run later on, or maybe to run many times.
To do this, you need to write a program. There are many, many different programming languages. Some were designed to make certain jobs easier. Others were designed for teaching. Still others were created because ... well, I don't know; people just like inventing new things.
Webol uses a small programming language that I have designed for teaching students. It is very similar to the first language I learned, called BASIC, which was created at Dartmouth University almost fifty years ago.
In Webol, you create a program by writing commands that start with a line number. Here is a simple program:
1 PRINT "Hello!"
2 PRINT "My name is Webby."
3 PRINT "I am a program."
Try typing that program into the Webol console. Notice that the computer echoes each line back to you, but does not actually run the line. You have now written a program: a set of instructions that the computer can follow.
LIST commandThe LIST command tells the computer to show us the
whole program.
Try it now:
LIST
RUN commandThe RUN command tells the computer to run the
program. It will look for all the instructions we gave, and will run
them, in the order of the line numbers.
CLEAR commandThe CLEAR command tells the computer to forget all the
lines that we taught it. After you say CLEAR, the computer
will not have a program to run until you give it a new program.
Program flow means the order in which the computer follows instructions. Like we saw above, normally Webol starts with the lowest line number, and runs each instruction exactly once. This lets us do many things. For example:
Write a program that counts from one to five. (You should be able to write this program in just five instructions).
Run the program to make sure that it works.
Soon, we will learn two new commands, GOTO
and IF, that will let us write programs that are much
more interesting. But first we need to learn about variables.
Variables are one of the most important parts of any program. They are a very important concept and you need to understand exactly what they do.
You can think of a variable as being a little box that is just big enough to hold one number. Each variable in your program has a name (like the name of the box) and a value (the number that is held in the box).
DIM to create a variableA new program begins with no variables. You need to register the
variables you want to use. You do this with the DIM
command. This command creates a new variable, gives it a name, and
puts the value 0 into it. Sounds complicated, but it is really quite
easy.
Create a variable named x:
10 DIM x
Create three variables named a, b, and c:
10 DIM a, b, c
Create a variable with a long name ThisIsMyVariable:
10 DIM ThisIsMyVariable
LET to change a variableOk, so now we know how to create a variable, and store a zero in
it. How can we store other numbers? Very easy; we use
the LET command. This command takes a variable name, and
a value to put into it.
Create x and give it the value 17:
10 DIM x
20 LET x = 17
Create variable sum and give it the total of adding a
few numbers:
10 DIM sum
20 LET sum = 1+2+3+4+5
You can also change the value of a variable and even use variables to create values for other variables:
1 DIM x,y
2 LET x = 1
3 PRINT "X=", x
4 LET y = x+1
5 PRINT "Y=", y
6 LET x = (x+y)/100
7 PRINT "Now x=", x
Can you predict what that program will print? Write it down, and now try to run the program. Were you right?
In programming, a string is what we call a piece of text. We write a string by putting it inside quotation marks. You've seen examples of strings already in some programs. We've used strings like "Hello" and "X=".
We can also put strings into variables, just like we did with numbers:
10 DIM name
20 LET name = "Alfred"
30 PRINT "Hello", name
What does that program do? Can you write a program that will say hello to YOU?
Now that you've learned about strings and about variables, can you answer the question we asked way back at the beginning? Why did PRINT "Hello" work, but not PRINT Hello?
GOTO commandUntil now, in every example we've seen, saying RUN has
caused each line of our program to run exactly one time, and always in
number order. The GOTO command makes things more
interesting by telling the program what line to run next..
WARNING: Look at the next program but do not run it. It will do bad things. So, read further first.
This program creates what we call an infinite loop, a program that runs forever and never stops.
10 PRINT "Here I am!"
20 GOTO 10
Real computers usually have ways to stop this kind of loop. But,
Webol does not have a good way yet. The only way to stop the program
is to press the browser's refresh button (the button that looks
like
or
). This will also erase
your program and reload all of Webol.
Try it now. Type in the two-line program and say RUN. When you have seen what it does, use the browser refresh button to stop it and clear the program.
You already know how to write a program that counts to 5:
10 PRINT 1
20 PRINT 2
30 PRINT 3
40 PRINT 4
50 PRINT 5
You now can also write a program that counts forever:
10 DIM count
20 LET count=1
30 PRINT COUNT
40 LET count=count+1
50 GOTO 30
Read that program. Can you understand what it does?
We are going to run this program a few times. But, it contains an infinite loop. So, let's save it first so that Webol reloads it automatically everytime we refresh the page. So, after you enter the program, push the Save button at the top of the page.
Now run the program and watch it count. When you've seen enough, push refresh to stop it.
What do you think would happen if we changed the program so that line 50 said GOTO 20? How about if we changed it to be GOTO 40? Try both of these changes and see if you guessed right.
IF commandSo, we know how to get the program to count to 5 or to count
forever. But, what if we want to count to 50? Would we have to write
a 50 line proram? NO! The very powerful IF command
gives us a neat way to do this.
What do you think this program will do? Try it.
10 DIM count
20 LET count = 1
30 PRINT COUNT
40 LET count = count+1
50 IF count <= 50 THEN GOTO 30
You've now learned enough about programming to write some simple, but very real, programs. You've learned:
PRINT command.DIM
and LET.GOTO
and IF commands.
Now, you can write some programs by yourself. Here are some ideas:
Write a program to print all the odd numbers less than 30.
Write a program to print the triangle numbers. These are the numbers that are the solutions to the equations: 1, 1+2, 1+2+3, 1+2+3+4, etc. So, the first numbers printed should be 1, 3, 6, 10, 15, etc.
Write a program to print the fibonacci series. This is the series of numbers you get if you start with 0 and 1, and each next number is the sum of the previous two. So, the series starts 0, 1, 1, 2, 3, 5, 8, 13, 21, etc. (2 is 1+1; 3 is 1+2; 5 is 2+3; etc.)
Invent your own programs. Have fun.
I will write the next chapter soon, but first I need to add more features to Webol.