This page describes in greater detail the core of the BASIC language used in LabTALK. Again it is not a tutorial about programming and it assumes users have some knowledge about programming. It is here more for reference and in form of examples. If you don't know what a for-next loop does then it won't help you to know its syntax.
Loop: for - next
The loop expects to be counting only upwards and its syntax is as in the example below
for i = 1 to 10
print i
next i
You can exit from loop anytime by simply overwriting the loop variable value
for i = 1 to 10
print i
if (i > 4) then
i = 10
endif
next i
loops can be also nested
for x = 1 to 10
for y = 1 to 10
print x,",",y
next y
next x
Condition: if -then- else -endif
A typical basic condition using if - then
a = 10
b = 20
if (a+b==30) then
print "true!"
endif
You have to always close if with endif!
a = 40
b = 20
if (a+b==30) then
print "true!"
else
print "false!"
endif
Inside the condition you can use these boolean operators and these operands:
Operand | Syntax example | Description |
& | if (a ==1 & b==2) then | AND |
| | if (a ==1 | b==1) then | OR |
== | if (a ==1) then | is equal |
!= <> |
if (a != 1 ) then if (a <> 1 ) then |
is not equal |
<= | if (a <=1 ) then | if less or equal |
< | if (a < 1 ) then | if less |
>= | if (a >= 1 ) then | if bigger or equal |
> | if (a > 1 ) then | if bigger |
Printing to screen: print, trace, print msg
Printing values of variables is done with print command. There are few flavors of it, each has the same syntax:
Command | How it works |
Output to Output window if run from within lab-TALK script editor If run directly (drag-and dropping) it will show a pop=up window |
|
trace | Output only to Output window. If run directly, nothing will be print. Good for debugging |
print msg | like print, but it will always pop-up message and never print to output window. The pop-up window will have "Continue Script?" thext added that allows to break the script run. |
The syntax of all print commands is simple. Consider the sample below:
a = 10
b = " Hello"
print "just text"
print a
print a+10
print "Variable:", a
print "Variable:", a ," String", b
Input command
This command is used to popout an input dialog where user can change one or more variables.
A simple usage is this:
a = 10
input "Set variable a", a
When you run the script a labTALK input dialog will be shown:
Obviously there places for more variables on the input dialog and indeed, you can use:
a = 10
b = "string"
input "Set variable a", a, "Set String", b
Running this will show:
Special input commands.
The input dialog can also modify the way how you enter the data.
Checkbox
This is done by setting CHECK: inside the string
a = TRUE
input "CHECK:Set checkbox", a
File box with browse button
This is done by setting FILE: inside the string
a = "C:\\myfile.txt"
input "FILE:Set file", a
Color box with color selection button
This is done by setting COLOR: inside the string
a = RGB(255,0,0)
input "COLOR:Set color", a
Option box (combo box)
The options follow after ':' and are divided by '|'. The variable is from 0 ...number of options -1
a = 1
input "Select Option:Option 1|Option 2|Option 3", a
In the above example, the return value (in "a") will be 0, 1 or 2.
A variable bCancelInput will become TRUE if user press Cancel on the input dialog.
A code below will exit the script if user press Cancel:
input "COLOR:Select Color ", color1
//allow cancel
if bCancelInput then
trace "Cancelled"
end
endif
end - finish script
The command end will simply finish the script
if bCancelInput then
end
endif
It is not necessary to put "end" at the end of script, but if you have subroutines, you have to put end before them, see gosub below.
goto - go to a label
The label is a number. Any number will do. Please do not confuse the labels with line numbers. It has nothing to do with them.
10 input "Value 1 or 2", a
if (a==1) then
goto 100
endif
if (a==2) then
goto 200
endif
print "I can understand 1 or 2"
goto 10
100 print msg "You entered One"
end
200 print msg "You entered Two"
end
The program will loop untill you enter 1 or 2 then it will display the message.
The labels don't have to be in sequential order, but it makes the program look more logical. (The numerical labels come from the time of first computers when every line written in BASIC had its own number, but we no longer use that so only the labels remained)
Gosub - go to subroutine
Subroutines in BASIC have to be at the very end of the script and must be labeled by label number (this has nothing to do with line numbers). Returning from a subroutine is done with the return command.
The printed result will be:
1
subroutine
2
Note the end command before the subroutine will finish the script. It is required to have an "end" statement before you write any subroutines.