Skip to main content

Flow Control

The following keywords and templates are used in Igor to control the sequential flow of execution in procedures.

AbortOnRTE

The AbortOnRTE flow control keyword raises an abort when a runtime error has occurred in a user-defined function.

You can place AbortOnRTE immediately after a command that might give rise to a runtime error that you want to handle instead of allowing Igor to handle it by halting procedure execution. Use a try-catch-endtry block to catch the abort, if it occurs.

AbortOnRTE has very low overhead and should not significantly slow program execution.

Details

In terms of programming style, you should consider using AbortOnRTE (preceded by a semicolon) on the same line as the command that may give rise to an abort condition.

When using AbortOnRTE after a related sequence of commands, then it should be placed on its own line.

When used with try-catch-endtry, you should place a call to GetRTError(1) in your catch section to clear the runtime error.

Example

Abort if the wave does not exist:

WAVE someWave; AbortOnRTE

See Also

Flow Control for Aborts and AbortOnRTE Keyword for further details.

The try-catch-endtry flow control statement.

AbortOnValue abortCondition, abortCode

The AbortOnValue flow control keyword will abort function execution when the abortCondition is nonzero and it will return the numeric abortCode. No dialog will be displayed when such an abort occurs.

Parameters

abortCondition can be any valid numeric expression using comparison or logical operators.

abortCode is a nonzero numeric value returned to any abort or error handling code by AbortOnValue whenever it causes an abort.

Details

Unlike AbortOnRTE, there is no need to call GetRTError(1) in the catch section of a try-catch-endtry to clear a runtime error, since AbortOnValue does not cause a runtime error.

See Also

Flow Control for Aborts and AbortOnValue Keyword for further details.

The AbortOnRTE keyword and the try-catch-endtry flow control statement.

break

The break flow control keyword immediately terminates execution of a loop, switch or strswitch. Execution then continues with code following the loop, switch or strswitch.

See Also

Break Statement, Loops, Switch Statements

catch

The catch flow control keyword marks the beginning of code in a try-catch-endtry flow control construct for handling any abort conditions.

See Also

The try-catch-endtry flow control statement for details.

continue

The continue keyword returns execution to the beginning of a loop, bypassing the remainder of the loop's code.

See Also

Continue Statement, Loops

default

The default keyword is used in switch and strswitch statements. When none of the case labels in the switch or strswitch match the evaluation expression, execution will continue with the code following the default label, if it is present.

See Also

Switch Statements

do-while

do
<body> // execute the loop body
while (<expression>) // as long as expression is TRUE

See Also

Loops, break

endtry

The endtry flow control keyword marks the end of a try-catch-endtry flow control construct.

See Also

The try-catch-endtry flow control statement for details.

for-endfor

for(<initialization>; <continuation test>; <update>)
<loop body>
endfor

A for-endfor loop executes the loop body code until the continuation test evaluates as false (zero) or until a break statement is executed in the body code. When the loop starts, the initialization expressions are evaluated once. For each iteration, the continuation test is evaluated at the beginning and the update expressions are evaluated at the end.

See Also

For Loop, Range-based For-Loop, break

for-var-in-wave

for(<type> varName : <wave>)	// Range-based for loop added in Igor Pro 9.00
<loop body>
endfor

A range-based for loop iterates over each element of a wave. The specified loop variable contains the value of the current wave element.

See Also

Range-based For-Loop, For Loop, break

if-else-endif

if (<expression>)
<TRUE part> // Execute if condition is TRUE
else
<FALSE part> // Execute if condition is FALSE
endif

See Also

Conditional Statements In Functions

if-elseif-endif

if (<expression1>)
<TRUE part 1> // Execute if condition 1 is TRUE
elseif (<expression2>)
<TRUE part 2> // Execute if condition 2 is TRUE and condition 1 is FALSE
[...]
[else
<FALSE part>] // Optionally execute if all conditions are FALSE
endif

See Also

Conditional Statements In Functions

if-endif

if (<expression>)
<TRUE part> // Execute if condition is TRUE
[else
<FALSE part>] // Optionally execute if condition is FALSE
endif

See Also

Conditional Statements In Functions

return [expression]

The return flow control keyword immediately stops execution of the current procedure. If called by another procedure, it returns expression and control to the calling procedure.

Functions can return only a single value directly to the calling procedure with a return statement. The return value must be compatible with the function type. Two values can be returned using a complex variable's real and imaginary parts. A function may contain any number of return statements; only the first one encountered during procedure execution is evaluated.

A macro has no return value, so return simply quits the macro.

See Also

The Return Statement

strswitch-case-endswitch

strswitch(<string expression>)  // string switch
case <literal><constant>: // execute if case matches expression
<code>
[break] // exit from switch
case <literal><constant>: // execute if case matches expression
<code>
[break]
[default: // optional default expression executed
<code>] // when no case matches
endswitch

A strswitch-case-endswitch statement evaluates a string expression and compares the result to the case labels using a case-insensitive comparison. If a case label matches string expression, then execution proceeds with code following the matching case label. When none of the cases match, execution will continue at the default label, if it is present, or otherwise the strswitch will be exited with no action taken. Note that although the break statement is optional, in almost all case statements it will be required for the strswitch to work correctly.

See Also

Switch Statements, break, default

switch-case-endswitch

switch(<numerical expression>)  // numeric switch
case <literal><constant>: // execute if case matches expression
<code>
[break] // exit from switch
case <literal><constant>: // execute if case matches expression
<code>
[break]
[default: // optional default expression executed
<code>] // when no case matches
endswitch

A switch-case-endswitch statement evaluates the numeric expression and rounds the result to the nearest integer. If a case label matches the numerical expression, then execution proceeds with code following the matching case label. When no cases match, execution continues at the default label, if present, or otherwise the switch exits with no action taken. Although the break statement is optional, in almost all case statements it is required for the switch to work correctly.

Literal numbers used as case labels are required by the compiler to be integers. Numeric constants used as case labels are rounded by the compiler to the nearest integers.

See Also

Switch Statements, break, default

try

The try flow control keyword marks the beginning of the initial code block in a try-catch-endtry flow control construct.

See Also

The try-catch-endtry flow control statement for details.

try-catch-endtry

try
<code>
catch
<code to handle abort>
endtry

A try-catch-endtry flow control statement provides a means for catching and responding to abort conditions in user functions. A programmatic abort is generated when the code executes Abort, AbortOnRTE or AbortOnValue. A user abort is generated when the user clicks the Abort button or presses the user abort key combination.

When code executes in the try-catch area, a programmatic abort immediately jumps to the code in the catch-endtry area rather than jumping to the end of the user function. A user abort jumps to the catch-endtry area when a flow control keyword such as for or while executes or at the end of the try code. Normal flow (no aborts) skips all code within the catch-endtry area.

Details

During execution of code in the catch-endtry area, user aborts are suppressed. This means that, if the user attempts to abort procedure execution by pressing the User Abort Key Combinations or by clicking the Abort button, this will not abort the catch code itself.

When an abort occurs, information about the source of the abort is returned via the V_AbortCode variable as follows:

-4:Abort triggered by AbortOnRTE
-3:Abort caused by Abort operation
-2:Stack overflow abort
-1:User abort
abortCode:Abort triggered by AbortOnValue.

See Also

Flow Control for Aborts and try-catch-endtry Flow Control for further details.

AbortOnRTE, AbortOnValue, Abort