The following table is a set of statements and functions for the script.
Type | Description |
| The |
| The |
| The |
| A for ([initialExpression]; [conditionExpression]; [incrementExpression]) { // instruction code } |
| Only one default statement can be used in the Additionally, |
| Terminates a |
| A block of code designed to perform a particular task and will only executes when something calls it. |
| Ends function execution and the function caller will return specifies a value. |
| The |
| The |
| When an external program is called directly, the caller will be in standby mode until the called program (the external program) ends its operation. However, if an external program is called using |
Example for if
…else
:
In this example, if tag A
equals 1, assign 100 to A
. Otherwise, assign 500 to A
.
if (tag.read("A") == 1) { tag.write("A", 100.0); } else { tag.write("A", 500); }
Example for while
:
The tag "A_WHILE" value will consistently be read and written to the variable val
in this example. If the value in the variable val
is less than 500, keep incrementing by 1 and store the incremented value to the tag "A_WHILE" in each loop. Once the variable val
equals 500, end the loop and exit the script.
var val = tag.read("A_WHILE"); while (val < 500) { val = val + 1; tag.write("A_WHILE", val) thread.msleep(10) }
Example for do
...while
:
The tag "DO_VAL" value will consistently be read and written to the variable do_val
in this example. The do
loop will increment by 1 and store the value into the tag “DO_VAL” before executing the while
loop. Once the variable do_val
equals 200, the while
loop will terminate, but the do
statement will stay executable allowing a user to increment by 1 manually.
var do_val = tag.read("DO_VAL"); do { do_val = do_val + 1; tag.write("DO_VAL", do_val) thread.msleep(100) } while (do_val < 200);
Example for for
In this example, the initial expression is 0. When i
is less than or equal to 100, i
will increment by 1 and store the result in A
every execution until i
equals 100. Once i
equals 100, the for
loop will end.
var i = tag.read("FOR_VAL"); for (i = 0; i <= 100; i = i + 1) { tag.write("FOR_VAL", i) thread.msleep(100) }
Example for switch
…case
…break
The tag "PET" value will consistently be read and written to the variable pet
in this example. The switch
will start matching the values between the variable pet
and the tag "PET." If the case matches, the expression in the case will execute accordingly. When finished, the break
statement will terminate the switch
or loop altogether.
var pet = tag.read("PET") switch (pet) { case "cat": tag.write("CASE_STR", "I love cats too!"); break; case "dog": tag.write("CASE_STR", "I love dogs too!"); break; }
Example for return
In this example, the function
keyword calls the values in myFunction
to perform multiplication and store the results to the tag "RETURN_ONE." The return
statement then outputs the value in the tag "RETURN_ONE."
var x = myFunction(4, 3); function myFunction(a, b) { a = a * b; tag.write("RETURN_ONE", a); return tag.read("RETURN_ONE"); }
Example for default
In this example, when the script executes, the tag "REPLY" will be assigned the string value "Hello, my name is CIMON. What is your name?" by default. If the user enters their name, the "NAME" string tag will store their name. If the name entered is "Christian," the "Hi Christian!" will update the "REPLY" string tag.
var name = tag.read("NAME") switch (name) { case "Christian": tag.write("REPLY", "Hi Christian!") break; default: tag.write("REPLY", "Hello, my name is CIMON. What is your name?"); }
Example for continue
The tag "TIMER" value will consistently be read and written to the variable timer
in this example. When the value in the variable timer
is less than 5000 and 10,000, the value in the variable timer
will be incremented by 101 each loop until it reaches 5000. When the value in the variable timer
reached 5000, the value in the variable timer
will be incremented by 1 instead. Once the value in the variable timer
reaches 10,000, the while loop will be terminated.
var timer = tag.read("TIMER"); while (timer < 10000) { timer = timer + 1; tag.write("TIMER", timer); thread.msleep(100) if (timer < 5000) { timer = timer + 100; tag.write("TIMER", timer); continue; thread.msleep(100) } }
Example for system.runScript()
In this example, the system.runScript
function will call and executes the script name “WHILE_Script.”
system.runScript("WHILE_Script")