<< Click to Display Table of Contents >> Navigation: 1. Starting Guides > 1. Starting Guide |
Starting Guide
Program Flow Control
How to script decisions
Program decisions (IF ... ELSE ... ENDIF
(Ixx) and Negative decisions (Nxx)
The script language supports a lot of "two way decision" commands. These are called "Ixx" because they always start with an "I" followed by two more letters. Two way decision commands let the script choose between two alternative ways.
In natural language we would say for example:
IF "File exists" THEN
... do something
ELSE (if the file does not exist)
.... do something else
ENDIF ' end of the statement
Translated to our script language this same logic would look like:
' This is a real filename we are looking for
VAR.$$FIL=?desktop\MRE.lnk
'IEF. = If Exists File (or folder if it ends with a "\")
IEF.$$FIL
' This will happen if the file exists
MBX.The shortcut is available.
ELS.
' This will happen if the file DOES NOT exist
MBX.The shortcut is not available.
EIF.
Often enough we only need a subset of the two-way decision. Especially if we only need to do something in one of the two cases. For example, if we need to delete a file only if it exists,
VAR.$$FIL=?desktop\MRE.lnk
IEF.$$FIL
' This will happen only if the file exists
DEL.$$FIL
EIF.
Now what, if we need to do something if the file does not exist only?
Of course we could write this:
VAR.$$FIL=?desktop\MRE.lnk
IEF.$$FIL
ELS.
' This will happen only if the file does not exist
DEL.$$FIL
EIF.
But that is not the proper way to do it.
Instead we can just change the "I" in "IEF." into an "N", making it to "NEF.". Like this:
VAR.$$FIL=?desktop\MRE.lnk
NEF.$$FIL
' This will happen only if the file does not exist
DEL.$$FIL
EIF.
This will work for all "Ixx" commands, not just for IEF. There are many such Ixx-commands like IEF., take a look into the help and try some of them. And also try the negative version "Nxx".
And what do i use, if i want to select between multiple choices?
You can use the Multiple-choice commands SCS. ... ESC. These commands are technically similar to SELECT CASE ... END SELECT statements in many other scripting languages. You can choose between string and numeric value comparison.
You always start with SCS.(variable)
In case of numeric comparison, you use CAN. (CAse Numeric). In case you prefer string comparison, using the standard-search patterns, CAS. (CAse String) is the instruction of your choice. See this example:
: $$TXT=Karl Kovse
SCS.$$TXT
' Use a part of the string or standard search patterns
CAS.Karl
PRT.This will come
CAS.Peter
PRT.This will not come
CAE.
PRT. This will not come
ESC.
MBX.Ready Sample 1
ENR.
Numeric comparison is done using the CAN. - instruction.
: $$VAL=1
: $$TXT=5
SCS.$$TXT
' As you can see, you can use formulas here
CAN.(2*2+$$VAL)
PRT.This will come
CAN.3
PRT.This will not come
CAE.
PRT. This will not come
ESC.
MBX.Ready
ENR.
Any formula that works with CAL. should also work with CAN. About CAS. you can use any search pattern, including variables. See this example:
: $$TXT=Karl Kovse
: $$STR=Karl&ANDTHEN:se
SCS.$$TXT
CAS.$$STR
PRT.This will come
CAS.Peter
PRT.This will not come
CAE.
PRT. This will not come
ESC.
MBX.Ready Sample 1
ENR.
Can i define my own control flow instructions with Macros?
Yes, Macros enable you to define your own instructions. Even Control-Flow Instructions.
Lets make an example.
: %SELECT_CASE 1
SCS.§§§01
END%
: %CASE 1
CAN.§§§01
END%
: %CASE_ELSE 0
CAE.
END%
: %END_SELECT 0
ESC.
END%
: $$TXT=Karl Kovse
: $$STR=Karl&ANDTHEN:se
' Now look how the final script looks.
' And it works exactly like before.
%SELECT_CASE $$TXT
%CASE $$STR
PRT.This will come
%CASE Peter
PRT.This will not come
%CASE_ELSE
PRT. This will not come
%END_SELECT
MBX.Ready Sample 1
ENR.
Lets see a more complex Macro Example. We want a command that checks if a variable is in a specified range, and if not corrects their values.
: %Check_Bounds 2
IVV.§§§01<§§§02
: §§§01=§§§02
EIF.
#IF PARAMS>2
IVV.§§§01>§§§03
: §§§01=§§§03
EIF.
#EIF
END%
: §§VAL=5
' Here we check only the lower bound
%Check_Bounds §§VAL|6
PRT. First Result is §§VAL
' Here we check the lower and the upper bound
%Check_Bounds §§VAL|1|4
PRT. Second Result is §§VAL
%Check_Bounds §§VAL|5|12
PRT. Third Result is §§VAL
MBX. Ready
ENR.
MBX.Ready Sample 1
ENR.
These Macros enable you to define your own instructions. You can define these Macros and put them into an separate file that you include to your script using JNF. This way you have them always at hand. Please note that MACROS are only expanded inside the robot.