Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Version History

« Previous Version 44 Next »

The following table is a set of statements and functions for the script.

Type

Description

ifelse Statement

The if statement executes the instruction code when the condition is true. Otherwise, the else statement will execute its instruction code. if...else statements can be nested inside another if...else statement using the else if statement which creates a nested loop. Please note, there is no limitation on how many else if can be nested inside if...else statement.

while Statement

The while statement execution will continue until the expression becomes false.

dowhile Statement

The do statement is executed at least once before while condition is evaluated. The loop will repeat until while condition becomes false.

for Statement

A for loop repeats until a specified condition becomes false. Below is the standard syntax used by a for loop:

for ([initialExpression]; [conditionExpression]; [incrementExpression]) {
    // instruction code
}

switchcase Statement

Only one default statement can be used in the switch statement. The switch expression is evaluated once by comparing the expression value with the values of each case. If matched, the associated instruction code is executed. Otherwise, default instruction code is executed.

Additionally, case statement must be followed by a constant value and terminated with a colon. Tags, variables, or expressions cannot be used in a case statement.

break Statement

Terminates a switch or a loop completely. The break statement prevents the execution of the following case in the switch statement. Adding a break function to the last case is good practice. If return is present, break statement is not necessary.

function Keyword

A block of code designed to perform a particular task and will only executes when something calls it.

return Statement

Ends function execution and the function caller will return specifies a value.

default Statement

The default statement is optional and does not need a constant. break statement is not needed either. This statement will execute if no cases get executed.

continue Statement

The continue statement terminates execution of the current iteration in a loop and then execute the next iteration in a loop. This statement does not terminate the loop completely.

system.runScript() Function

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 runScript(), the caller will not wait until the end of the called program. Both the caller and called program will be executed in parallel.

Example for ifelse:

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 switchcasebreak

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."

function myFunction(a, b) {
	a = a * b;
	tag.write("RETURN_ONE", a);
	return tag.read("RETURN_ONE");
}
var x = myFunction(4, 3);

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")
  • No labels