> Sford wiki > Sford Basic tutorial > Sford Basic tutorial 4

1 - 2 - 3 - 4

Let's get back the part 3 program ("File"->"Open", use "Celia" folder, select "age1"). List it out:


It should look like this:

3 print "What is is your name?"
4 input name$
7 print "How old are you?"
8 input age
10 legal_age = 21
20 print name$; " must be ";legal_age;" to drink"
30 print name$; " must wait ";legal_age-age;" years"
40 goto 3

Now run it and enter 30 as the age.

Oops! Negative numbers. How do we deal with that? New command time!

25 if age > 20 then goto 50
50 print name$; " can drink"
60 goto 3

The if command tells the computer to do something conditionally. If age is greater than 20, skip lines 30 and 40. When you are inside an if command, you usually put some kind of comparison between the if and the then. If the comparison is true, then the computer will do the command after the then.

Now run it and try some different ages.

Let's put one more in so that we don't have to mess with the menus to get it to stop:

9 if age = 0 then stop

Now, instead of using the menus to stop the program, you can just type 0 when the program asks you for age, and the program will stop.

List the program and take note of something. In line 10, legal_age = 21, this line actually does something. It stores the number 21 into the variable legal_age.

But in line 9, if age = 0 then stop, this is not putting the value 0 into age. Since this is inside the if command, it is comparing the variable age to zero to see if they are equal (i.e. if you typed 0 for your age). If so, then it does the then part, which stops the program.

One last thing. Notice that you now have lines 7, 8, 9 and 10. What if you want to stick a line in between one of those? You can't! The whole reason people usually start numbering their program by 10s is to allow for adding lines in the middle. But even with that, we now have a section which is numbered too tightly. Do this:


Taa daa! It is now renumbered. And look! Lines 100 and 120 used to be goto 3 and are now goto 100. And line 80 used to be ... then goto 50 and is now ... then goto 110. So not only does renum change the line numbers, it also changes references to line numbers. So your program still works properly

Save it as "age2".

You now know how to program!

Here are the commands that you have learned:

The next commands are usually put inside a program.

With these commands, you can write very powerful programs. There are other commands which make writing certain kinds programs easier, but they are usually not necessary. If you are interested in writing more-advanced programs, I would probably have you learn a different computer language

Retrieved from "http://wiki.geeky-boy.com/w/index.php?title=Sford_Basic_tutorial_4"