EBasic Introduction

EBASIC is a small version of the BASIC language, which was very popular in the 80s with home computers. It is very easy to learn, and is quite flexible. It was this simplicity that made it the best choice for this system.

The language uses a number of English like command words to tell the computer what to do. You build up a program by selecting the appropriate commands and forming them into lines of code. Each code line starts with a line number. The computer reads and executes the commands in line number order.

Why use line numbers?

Two reasons really, firstly, it is easy to refer to a line of code, to change it or instance, by it's line number.

The second reason is that be can instruct the computer to jump to a line number in a program, if we want it to execute the lines of code that follow from that number.

Entering a program

The first thing that needs to be done is to tell the computer that you are going to start entering a new program, you do this type typing:-

NEW, and press the enter key.

To enter a line of program code, type the line number followed by a space, and then the command, end press the enter.

10 PRINT "Hello World"

To run this program type RUN, and press enter.

The output of this program would be:-

>Hello World
>

To make changes to the program, you only need to retype the line that would wish to change. Typing :-

10 PRINT "Goodbye World"

Would alter the program, so that running it would give:-

>Goodbye World
>

You can see a complete listing of the program at any time by entering the command:-

LIST

To delete a line from the program, just enter the line number with nothing else.

Looping

It is easy to make something repeat forever by creating a loop. The easiest way to do this is to use the GOTO command:-

10 PRINT "This will repeat forever"
20 GOTO 10

This program will fill the screen with the same message until the program is stopped.

Simple Maths

You can do simple maths work in your programs too, from simple arithmetic:-

10 PRINT 5+5+2

Which would print out the result 12

To something resembling algebra like:-

10 A=5
20 B=2
30 PRINT A*B + 2

Which would print the result 12

So, A*B means, A times 5, and + 2 is added to the result.

There are only a handful of maths operators that need to learned, these are as follows:-

+ Add
- Substract
/ Divide
* Multiply
MOD - Modulus, find the remainder after a division
DIV - Do a division, ignoring any remainder and just showing the whole number result

Here is an example of DIV and MOD

PRINT 10 DIV 7

Would return 1 because 7 goes into 10 1 time

PRINT 10 MOD 7

Would return 3 because after dividing 10 by 7, there is a remainder of 3. Or writing it another way:-

7 * 1 + 3 = 10

Taking Input

Sometimes you need to prompt the user of your program for some information, perhaps the users name or their age. To do this, the INPUT command can be used. The example below prompts the user for their name:-

10 PRINT "Hi, what is your name?"
20 INPUT name$
30 PRINT "Pleased to meet you ", name$

The INPUT command will wait for the user to type something and press enter. Whatever the user typed will be stored in the name$ variable. The PRINT command is able to print more that one thing at a time, this is why there is a comma between "Pleased to meet you" and name$.

Also notice that there is no quotation marks around name$, this makes the PRINT command print out the contents of the name$ variable. Anything in quotes is know as a string literal and will just print what is between the quotes.

Variables

We have touched on variables by example above, but now it is time to learn a bit more about them. Variables are like little places where information can be stored while a program is running. There are two main types of variable that can be created and this will determine what you can do with them.

Numeric variables, these are all about storing number values. They are very useful for doing calculations and controlling things, like for instance lives lost or score in a game. Numeric variables can be mixed with each other and anything else that generates a number to produce a new number. Usually, you'll want to assign the result of all this to a new variable, so that it can be used in something else.

String variables, these are for storing anything that can be typed or printed. Perhaps, a users name or instructions to a person for reading like 'walk forward'. You can't do any maths will string variables but you can combine them and split them and generally have fun with words.

Variable naming

Generally, a variable can be named with any number of characters up to a limit of 8. Only a-z,0-9 and _ underscores are permitted. The first character1 of a variable must be a letter.

1) A character is anything that can be typed by pressing a single key

Assigning values to variables

Setting a value to a variable is a simple matter of putting the variable name equals to the value. If the variable does not exist, it will be created automatically.

See the examples below

a = 5

creates the variable a and sets it to the number 5

a = 6

Set the value of a to 6.

a = a + 1

Increase the value of a by 1. (increment)

a = a - 1

Decrease the value of a by 1. (decrement)

a = a * 2

Double the value of a

n$ = "Hello"

Crease the string variable n$ and set it to the string "Hello"

n$ = n$ + " there"

Append " there" to the variable n$. Meaning that n$ now contains "Hello there"

TRUE and FALSE and boolean logic

True and false are simple logic values, and basically mean YES for true and NO for false. Some commands require a logic input that evaluate to either true or false. Also, some expressions evaluate to a logic result, unlike the usual numeric result.

Here are some examples of expressions that give a logic result:-

A=B (A equals B)
A<B (A is less than B)
A>B (A is greater than B)
A<>B (A is not the same as B)

Notice that they are all comparisons expressions, the answer from each one is true or false.

The following line would always print "YES"

IF (5>4)=TRUE THEN PRINT "YES"

If Then and Else

Almost all programs need the ability to make decisions based on some sort of input and perform actions. The IF keyword is what makes this happen. There are a few different ways in which this can be used but the simplest is shown below.

10 IF 5>4 THEN PRINT "Five is greater than four"

The expression that follows the IF statement must evaluate to true or false. 5 is greater than 4 (>) so the result is true. The statement following the THEN keyword will only run is the expression was true.

Here is another example, using variables and input

10 age = 10
20 PRINT "Try to guess my age"
30 INPUT guess
40 IF guess=age THEN PRINT "Well done, you guessed correctly!"
50 IF guess<age THEN PRINT "Sorry, your guess was too low"
60 IF guess>age THEN PRINT "Sorry, your guess was too high"

Another form of the IF THEN statement is to also use the ELSE keyword. This allows you to specify what you want to happen if the original expression is false. See the example below

IF guess=age THEN PRINT "Well done" ELSE PRINT "Sorry, you didn't guess right"

The above example works of for a yes or no type result, but doesn't quite fit our game guessing game. Fortunately, there is the possibility to replace the ELSE with an ELSE IF which would allow us to specify another expression to test if the first one was false. So the example below uses this to with us the same features as the original guessing program.

10 age = 10
20 PRINT "Try to guess my age"
30 INPUT guess
40 IF guess=age THEN PRINT "Well done, you guessed correctly!" 
ELSE IF guess<age THEN PRINT "Sorry, your guess was too low"
ELSE PRINT "Sorry, your guess was too high"

Notice how we didn't have to specify the greater than test (>) because of the simple rules of deduction. We know that if the guessed number wasn't equal and also wasn't less, then the only remaining possibility would be greater than.

Remember, you are not stick with using the PRINT command with IF THEN blocks. You can put any other meaningful statement you want. You could put in a variable assignment or perhaps jump to another part of the program using the GOSUB or GOTO command. It is also possible to put in more than one command too, you do this by adding a colon (:) to the end of the first command and following it with a second. See the example below:-

40 IF guess=age THEN PRINT "well done, you guessed correctly!":age=age+2:GOTO 20

Can you guess what would happen if the program was run with this line change?

FOR LOOPS

FOR loops are a way to make some code repeat a number of times based on the counting up (or down) of a variable. This has many uses, one such use is to draw many lines on the screen.

The loop is started with the FOR declaration and then followed by a variable assignment, this assignment will define the starting value of the loop control variable. Following this the TO keyword which is followed by the end value of the loop. Optionally, a STEP keyword can follow which would then be followed by the amount that the loop control variable must change by at the end of each run of the loop body. So a simple example of a loop that counts up from 1 to 10 is shown below:-

10 FOR X=1 TO 10
20 NEXT

Another example of a loop that counts backwards from 10 to 1 is show below:-

10 FOR X=10 TO 1 STEP -1
20 NEXT

In the above example, the STEP keyword was to be included to tell the computer that it needs to add -1 to the control variable at each execution.

It is worth noting here that, running these programs would show nothing on the screen and there is no output. The program would simply loop from lines 10 to 20 10 times. This would happen very very quickly. In order to see something for your effort, perhaps putting in a PRINT statement to see what the loop control variable is doing, as shown in the example below:-

10 FOR X=1 TO 1
20 PRINT X
30 NEXT

REPEAT UNTIL LOOPS

These loops work by repeating all the code between the REPEAT and UNTIL keywords, until the expression following the UNTIL keyword evaluates to true.

With this kind of loop, you are not limited to using a variable counter to control the end of the loop. You can use anything that will result to true or false.

Here is an example that uses a loop to repeatedly ask the user for names until quit is entered.

10 REPEAT
20 PRINT "Please enter a name"
30 INPUT name$
40 UNTIL name$="quit"

SUBROUTINES

Is is easily possible to divide up your program into smaller chunks that can be reused by other parts of the program. A good example of this is a subroutine that clears the screen and prints a menu.

The GOSUB command tells the computer to jump to the line number that follows. It will then execute the commands it finds until it encounters a RETURN command. This will make it jump to the line following the original GOSUB command.

The example program below has a sub routine defined at line 200, which prints a menu on the screen.

10 GOSUB 200
20 PRINT "Enter command"
30 REPEAT
40 INPUT cmd$
50 IF cmd$="S" THEN PRINT "You selected S"
60 IF cmd$="Q" THEN PRINT "You selected Q"
70 GOSUB 200
80 UNTIL cmd$="Q"
90 PRINT "Program ended"
100 END
200 CLS
210 PRINT "MENU"
220 PRINT "S - Start"
230 PRINT "Q - Quit"
240 RETURN

(c) 2017 PicoCreate

Terms and conditions apply, click here for further details