Execution of Programs
Comments
Variables
Integer Variables
Floating Point Variables
String Variables
Arrays
Boolean Expressions
The If Construct
The For Construct
The While Construct
The Repeat Construct
The Goto Statement
The End Statement
Procedures

If you have done some programming before then you might want to skip this tutorial. We are going to cover the basics of writing a program in TNT Basic.

Execution of Programs
Any program written in TNT Basic is built from a sequence of commands. E.g.

Command 1
Command 2
Command 3
.
.
.
Command 200
etc...

When the program is executed (run) these commands are read in by the computer and obeyed one by one. The computer starts from the top of the program and works its way down through the program one command at a time. When it reaches the end of the program it stops.

Comments
When writing programs they can easily begin to look confusing and it can be difficult to understand what the code is supposed to be doing. To help you out TNT Basic allows you to type anything you like into the program and get the computer to ignore it. e.g.

' This is a comment and it will be ignored

A comment must always have an apostrophe at the beginning of the line. TNT Basic shows you that it understands it should be ignored by displaying it in red.

Variables
Computers would be very little use to us if they could not store information. TNT Basic programs can store information in something that is known as a variable. There are three types of variable support in TNT Basic, these are integers, floating point numbers and strings.

It is usually easier to think of a variable as a box. The box has a label on the front of it so it can be identified and the box can also have one item stored inside it.

A variable has to be created before it can be used. This means you have to define what kind of information the variable can hold (integer, floating point or string) and what the name of the variable is. E.g.

int age
float height
string name

This defines three variables, age can hold any whole number (...,-4, -3, -2, -1, 0, 1, 2, 3, 4, ...), height can hold any decimal point number (1.87, 2.00000, 53.56987,...) and name can hold any sequence of letters ("Andrew","Bob","Carl","1234","!@£$").

Integer Variables
An integer variable can be used to contain any whole number (positive or negative). E.g.

int number=5

This line creates a variable with the name number and puts the value 5 into it.

Try typing this in...

int number=20
Print number

When this program is executed it outputs the value 20. That is because the value contained inside number is printed to the screen. Since number contains 20, that is what gets printed.

int number=20
Print number+5

The output from this program is 25 because it takes the contents of number and adds 5 onto it.

The contents of a variable do not have to be constant. The value of a variable can be set to whatever you like whenever you like. E.g.

int number=10
number=number+10
Print number

This will also output 20 because, although the value of number begins at 10, it is increased by 10 and then output.

To a mathematician this line may look confusing...

number=number+10

What this means is that it will extract the value of number, add 10 to it and then place it back into the variable number. In short, it increases the value of number by 10.

Any other mathematical operation can be performed on an integer. E.g.

number=number-10 ' Subtraction
number=number/2 ' Division
number=number*2 ' Multiplication

Any number of variables can be used in an operation.

int a=4,b=3,c=2,d
d=a+b-c

After this has been executed the value of d is 5 (4+3-2).

Brackets can also be used in maths operations to tell the computer what order it should perform sums in. e.g.

int a=4,b=4,c=2,d
d=a*b+c

This can be slightly ambiguous because this operation could mean...

d=(a*b)+c

or

d=a*(b+c)

In this case, the computer would choose the first option because multiply (*) has a higher precedence than addition (+). Precedence is decided like this...

*,/ Highest
+,- Lowest

If you want the computer to perform a certain part of the sum before anything else then place brackets around it.

Floating Point Variables
A floating point variable works the same as an integer except it can hold any decimal number.

float a=3.14
Print a

This program would output "3.14" because that is the value inside a. Since a is a float it can store the part of the number after the decimal point.

int b=3.14
Print b

This program would output "3". This is because b can only hold whole numbers (no decimal points). When a floating point number is placed inside an integer the value is truncated (i.e. only the part before the decimal point is used).

Floating point variables should only be used if the accuracy is necessary (e.g. measurements). If only whole numbers are going to be stored then use an integer (e.g. Age).

String Variables
A string variable can hold a sequence of characters (letters or symbols). These can be used to hold textual information.

string name="Bob"
Print name

This program will output "Bob".

Strings can also be added together.

string firstName="Bob", secondName="Dylan"
Print firstName+" "+secondName

This program will ouput "Bob Dylan" because it has been built up from adding all the different strings together in that order.

Strings can also be tested for equality just like integers and floats. E.g.

string a="Hello",b="Hello"

if a=b
Print "These strings match"
else
Print "These strings do not match"
end if

Arrays
An array can hold together a collection of variables (of any type) together under a single name. Think of it as a room filled with boxes; the room has a name and each box in the room has a number.

An array is defined like this...

int n[]
Dim n[10]

Or

int n[10]

The number in the square brackets indicates the number of elements (variables) that should be created. In this example, there would be 10 integers contained inside n.

Arrays of floats and strings can also be created.

float number[10]
string name[10]

Values can be placed in and taken out of the array just like ordinary variables. The only difference is that an index into the array is required so the computer knows which particular element you want to access. The index is from zero, this means that if an array is defined to have 10 elements in it the first element will be 0 and the last element will be 9. E.g.

n[5]=7

This gets element 5 from array n and assigns it the value 7.

Print n[5]

This gets element 5 from array n and outputs its value.

Only the elements contained in the array can be accessed. If you attempt to access an array element that is not in the array TNT Basic will cause an error. E.g.

int n[10]
n[10]=7

This causes an error because n only has 10 elements and (since the index starts from zero) the last element of n is 9. This means the array index is "Out Of Bounds".

Boolean Expressions
A Boolean expression is a mathematical expression that can only ever evaluate to true or false. E.g.

a<b

This expression will return true if the value contained inside a is less than the value contained inside b. Otherwise it will evaluate to false. It will not evaluate to anything else.

A Boolean expression can be built using any of the following:

> Greater than
>= Greater than or equal to
= Equal to
<= Less than or equal to
< Less than

They can also be combined to make more complicated expressions. E.g.

a<b and c<d

In this example, the expression will only evaluate to true if a is less than b AND c is less than d.

a<b or c<d

This expression will only evaluate to true if a is less than b OR c is less than d.

not (a=b)

This expression will evaluate to true if a does NOT equal b. The not operator reverses the true/false value of a Boolean expression.

Any ordinary numerical expression can also be used as a Boolean expression. If the numerical expression evaluates to zero then it is false, otherwise it true.

5+7
5

These numerical expressions don't evaluate to zero so therefore they are true.

7-3-4
0

These numerical expressions do evaluate to zero so therefore they are false.

The If Construct
The if construct is a simple statement which can decide whether or not a piece of code should be executed. In its most basic form it looks like this:

if booleanExpression
...
end if

The code inside the if statement is only executed if the Boolean expression is true.

If statements can also have an else case attached. E.g.

if booleanExpression
...
else
...
end if

The first piece of code is executed if the Boolean expression is true, otherwise the piece of code after the else statement is executed.

If statements can also have 'else ifs' attached. E.g.

if conditionA
... ' Section A
else if conditionB
... ' Section B
else if conditionC
... ' Section C
else
... ' Section D
end if

If conditionA is true, Section A only is executed.
Otherwise, if conditionB is true, Section B is executed.
Otherwise, if conditionC is true, Section C is executed.
If none of the above conditions are true then Section D is executed.

The For Construct
The For construct is a simple statement which executes a piece of code a set number of times. It also changes the value of a variable on each iteration (loop) through the construct.

It looks like this...

for n = 1 to 10
...
next n

The first time through the construct, n is set to 1. The code contained inside the for construct is then executed repeatedly until n equals 10. The for statement automatically increases the value of n by one each time through the loop. However, this can be changed by adding a step operator. E.g.

for n = -10 to 10 step 2
...
next n

In this example, n starts off as -10 and is then increased by 2 each time until it reaches the value 10. If no step value is specified then it is assumed that the step has a value of 1.

The While Construct
The While construct is a simple statement which decides whether or not a piece of code should be executed.

It looks like this:

while condition
...
wend

The code contained inside the while statement is executed while the condition is true.

If the condition is true then the code is executed. The condition is then tested again and, if it is true, the code is executed again. This process continues until the condition eventually becomes false.

int x=0
while x<10

> x=x+1

wend

This will execute the body of the while construct 10 times before x<10 eventually evaluates to false.

Note: If the condition is false to begin with, then the code is never executed.

This construct can loop forever (infinite loop) if the body of the while loop does not eventually change the condition so that it evaluates to false.

The Repeat Construct
The Repeat construct is a simple statement which executes a piece of code until a certain condition becomes false.

It looks like this:

repeat
...
until condition

The only difference between this and the while construct is that the code is executed before the condition is tested. This means that it will always be executed at least once.

Repeat statements can also be constructed into an infinite loop like this:

repeat
...
forever

The Goto Statement
The goto statement simply moves the flow of control over to a label. e.g.

...
Hello:
...
goto Hello
...

Goto will execute an unconditional jump to the destination. In order to place a condition on the jump it could be wrapped inside an if construct.

Goto should not really be used in programs at all. They have a tendency to make code unstructured and they make programs difficult to manage when they become significantly large. It is much better practice to use the other flow control constructs and procedures.

The End Statement
The end statement is simply a command that terminates the TNT Basic program where it is. No more code will be executed after this command has been called.

It can be called by typing...

End

...at any point in the program.

Procedures
A procedure is a section of code that is separate from the rest of the main body. It can be called from other points in the code in order to execute it. e.g.

' Main Body of the code
...
HandleOutput()
...
' Procedure Definition
Procedure HandleOutput()
...
End Proc

In this example, the code that is written inside the body of HandleOutput will be executed at the point from where it is called. The procedure definition must be placed after the end of the main body of code.

Procedures are very useful because they allow the code to be broken down into smaller pieces which are simpler to understand.

Local Variables
Inside each procedure the variables are completely separate from all the other variables throughout the programs. The variables used inside the program cannot be used inside the procedure and vice versa.

Variables inside the procedure are known as local variables because they are 'local' just to that procedure. They can have the same name as variables outside the procedure and they will not interfere with each other (they behave exactly like two completely different variables).

Passing Values
Since procedures do not have access to variables outside of their main body, in order to give values to the procedure they can be passed in when it is called.

...
Test(5,n,name)
...

Procedure Test(int number,int otherNumber,string name)
...
End Proc

Inside the procedure Test:
- The variable 'number' will contain the value 5.
- The variable 'otherNumber' will contain the same value of n
- The variable 'name' will contain the value of name from the point where the procedure was called.

The values are 'copied' into the variables inside Test. This means that the variables can be changed and used just like all the other variables inside Test. If the values of the variables are changed they do not affect the values in the main body of code.

Returning Values
Procedures can return values to the point in the code from which they were called. e.g.

' Main code
...
Test()
Print Result Int
...

' Procedure definition
Procedure Test()
...
End Proc(n)

Returning values from procedures is useful because it is a convenient way of getting a result from a procedure. Only one value can be returned from a procedure. The value you choose to return is specified in the brackets after the End Proc statement.

The procedure Test can change the value of n and return it. This value can then be caught from the point the procedure was called from using the Result command.

Result Int/Result Integer c ontains the returned value if the value returned was an integer.
Result Float contains the returned value if the value returned was an floating point number.
Result Str/Result String c ontains the returned value if the value returned was a string.

Global Variables
Global variables are different to other variables because they can be accessed throughout the entire program regardless of whether they are being accessed from a procedure or not. To make a variable global (e.g. n) it needs to be declared to be so.

int n
Global n

This can also be written as...

Global int n

Once this has been done it can now be accessed throughout the main code and inside all procedures.

Shared Variables
Instead of making a variable global to all procedures, it is possible to make that variable accessible only to the procedures that need it.

...
String output
ComposeHello()
Print output
...

Procedure ComposeHello()
Shared String output
output="Hello"
End Proc

This piece of code will output the string "Hello". This is because it has been changed inside the procedure ComposeHello which gained access to it using the Shared command.
This technique makes code much neater because only the procedures that need the variables will have access to them.

Recursion
A procedure is able to call other procedures from inside it. It is also able to call itself from inside it. This is known as Recursion.

Procedure Recurse()
Recurse()
End Proc

This procedure repeatedly calls itself. This, however, would cause an infinite loop because it would just keep calling itself and would never stop.

In order to prevent this from happening recursive procedures need to be able to keep track of how many times they have been called.

Procedure Recurse(int n)
if n>0
Recurse(n-1)
end if
End Proc

This would only call the procedure n times because each time it is called the value of n is reduced. Once n becomes less than or equal to 0, the procedure is no longer called and termination of all the called procedures would begin.

Table of contents