2.3.1.4 Conditional and Loop Structures

The structure of the LabTalk language is similar to C. LabTalk supports:

  • Loops, which allow the program to repetitively perform a set of actions.
  • Decision structures, which allow the program to perform different sets of actions depending on the circumstances.

Loop Structures

All LabTalk loops take a script as an argument. These scripts are executed repetitively under specified circumstances. LabTalk provides four loop commands:

Command Syntax
repeat repeat value {script};
loop loop (variable, startVal, endVal) {script};
doc -e doc -e object {script};
for for (expression1; expression2; expression3) {script};

The LabTalk for-loop is similar to the for loop in other languages. The repeat, loop, and doc -e loops are less familiar, but are easy to use.

Repeat

The repeat loop is used when a set of actions must be repeated without any alterations.

Syntax: repeat value {script};

Execute script the number of times specified by value, or until an error occurs, or until the break command is executed.

For example, the following script types the string three times:

repeat 3 { type "line of output"; };

Loop

The loop loop is used when a single variable is being incremented with each successive loop.

Syntax: loop (variable, startVal, endVal) {script};

A simple increment loop structure. Initializes variable with the value of starVal. Executes script. Increments variable and tests if it is greater than endVal. If it is not, executes script and continues to loop.

For example, the following script outputs numbers from 1 to 4:

loop (ii, 1, 4)  {type "$(ii)";};

Note: The loop command provides faster looping through a block of script than does the for command. The enhanced speed is a result of not having to parse out a LabTalk expression for the condition required to stop the loop.

Doc -e

The doc -e loop is used when a script is being executed to affect objects of a specific type, such as graph windows. The doc -e loop tells Origin to execute the script for each instance of the specified object type.

Syntax: doc -e object {script};

The different object types are listed in the document command.

For example, the following script prints the windows title of all graph windows in the project:

doc -e P {%H=}

For

The for loop is used for all other situations.

Syntax: for (expression1; expression2; expression3) {script};

In the for statement, expression1 is evaluated. This specifies initialization for the loop. Second, expression2 is evaluated and if true (non-zero), the script is executed. Third, expression3, often incrementing of a counter, is executed. The process repeats at the second step. The loop terminates when expression2 is found to be false (zero). Any expression can consist of multiple statements, each separated by a comma.

For example, the following script output numbers from 1 to 4:

for(ii=1; ii<=4; ii++)  
{
   type "$(ii)";
}

Note: The loop command provides faster looping through a block of script.

Decision Structures

Decision structures allow the program to perform different sets of actions depending on the circumstances. LabTalk provides three decision-making structures: if, if-else, and switch.

  • The if command is used when a script should be executed in a particular situation.
  • The if-else command is used when one script must be executed if a condition is true (non-zero), while another script is executed if the condition is false (zero).
  • The switch command is used when more than two possibilities are included in a script.

If, If-Else

Syntax:

  1. if (testCondition) sentence1; [else sentence2;]
  2. if (testCondition) {script1} [else {script2}]

Evaluate testCondition and if true, execute script1. Expressions without conditional operators are considered true if the result of the expression is non-zero.

If the optional else is present and testCondition is false (zero), then execute script2. There should be a space after the else. Strings should be quoted and string comparisons are not case sensitive.

Single statement script arguments should end with a semicolon. Multiple statement script arguments must be surrounded by braces {}. Each statement within the braces should end with a semicolon. It is not necessary to follow the final brace of a script with a semicolon.

For example, the following script opens a message box displaying "Yes!":

%M = test;
if (%M == "TEST")  type -b "Yes!";
else type -b "No!";

The next script finds the first point in column A that is greater than -1.95:

newbook;
col(1)=data(-2,2,0.01);
val = -1.95;
get col(A) -e numpoints;
for(ii = 1 ; ii <= numpoints ; ii++)
{
   // This will terminate the loop early if true
   if (Col(A)[ii] > val)  break;
}
if(ii > numpoints - 1)
   ty -b No number exceeds $(val);
else
   type -b The index number of first value > $(val) is $(ii)
The value is $(col(a)[ii]);

It is possible to test more than one condition with a single if statement, for instance:

if(a>1 && a<3) b+=1;  // If true, increment b by 1

The && (logical And) operator is one of several logical operators supported in LabTalk.

Switch

The switch command is used when more than two possibilities are included in a script. For example, the following script returns b:

ii=2;
switch (ii)
{
     case 1:
         type "a"; 
         break;
     case 2:
         type "b"; 
         break;
     case 3:
         type "c"; 
         break;
     default:
         type "none"; 
         break;
}

Break and Progress Bars

LabTalk provides a break command. When executed, this causes an exit from the loop and, optionally, the script. This is often used with a decision structure inside a loop. It is used to protect against conditions which would invalidate the loop test conditions. The break command can be used to display a progress status dialog box (progress bar) to show the current progress through the loop.

Exit

The exit command prompts an exit from Origin unless a flag is previously set to prevent the exit.

Continue

The continue command can be used within loops. When executed, the remainder of the loop is ignored and the interpreter jumps to the next iteration of the loop. This is often used with a decision structure inside a loop and can exclude illegal values from being processed by the loop script.

For example, in the following for loop, continue skips the type statement when ii is less than zero.

for (ii = -10; ii <= 10; ii += 2) 
{
   if (ii < 0)
     continue;

   type "$(sqrt(ii))";
}

Sections in a Script File

In addition to entering the script in the Label Control dialog, you can also save it as an Origin Script (OGS) file. An Origin script file is an ASCII text file which consists of a series of one or more LabTalk statements. Often, you can divide the statements into sections. A section is declared by a section name surrounded by square brackets on its own line of text:

[SectionName]

Scripts under a section declaration belong to that section until another section declaration is met. A framework for a script with sections will look like the following:

...
Scripts;
...
[Section 1]
...
Scripts;
...
[Section 2]
...
Scripts;
...

Scripts will be run in sequence until a new section flag is encountered, a return statement is executed or an error occurs. To run a script in sections, you should use the

run.section(FileName, SectionName)

command. When filename is not included, the current running script file is assumed, for example:

run.section(, Init)

The following script illustrates how to call sections in an OGS file:

type "Hello, we will run section 2";
run.section(, section2);

[section1]
type "This is section 1, End the script.";

[section2]
type "This is section 2, run section 1.";
run.section(, section1);

To run the script, you can save it to your Origin user folder as test.ogs, and type the following in the command window:

run.section(test);

If code in a section could cause an error condition which would prematurely terminate a section, you can use a variable to test for that case, as in:

[Test]
SectionPassed = 0;
// Here is where code that could fail can be run
...
SectionPassed = 1;

If the code failed, then SectionPassed will still have a value of 0. If the code succeeded, then SectionPassed will have a value of 1.