spec

Software for Diffraction

1.3.5. - Flow Control



Flow control allows you to construct complex scans and other macros to control experiments and take data. The syntax of the flow control is very similar to standard C. For example, to list all the motor positions, you can use the following loop:
1.FOURC> for (i = 0; i < MOTORS; i++) {
2.more>     printf("Motor %d %-10s = %g\n", i, motor_name(i), A[i])
3.more> }
Motor 0 Two Theta = 3 Motor 1 Theta = 1.5 Motor 2 Chi = 0 Motor 3 Phi = 0 4.FOURC>

As in C, the for statement contains within parentheses three optional expressions separated by semicolons. The first expression is executed before entering the loop. The second is the test done before each pass of the loop -- if it evaluates false, the loop is terminated. The third expression is executed at the end of each loop.

The conditional statements
if ( condition ) statement
and
if ( condition ) statement else statement
are also available. For example, to test whether a variable has been assigned a value, you could examine the return value of the built-in whatis() function (described 2.4.1.2 in the Reference Manual).
1.FOURC> if (whatis("DATAFILE")>>16&0x8000)
2.more> print "Warning, Data file is uninitialized!
3.more> ;
Warning, Data file is uninitialized! 4.FOURC>

When there is a solitary if statement, a semicolon, extra newline or some other command must be read before the if statement will be executed, as it is not clear to the parser whether an else statement will follow. If there is an else statement, it must follow the if portion of the statement on the next line.
1.FOURC> if (whatis("DATAFILE")>>16&0x8000)
2.more> print "Warning, Data file is uninitialized!
3.more> else
4.more> print "Data is being stored in", DATAFILE
Data is being stored in /usr/alan/default.dat 5.FOURC>



The while construction is also available. Usage is
1.FOURC> while (wait(0x22)) {
2.more> getangles
3.more> printf("%10.4f\r", A[tth])
4.more> }
5.FOURC>



As in C, continue and break statements may be used within loops. The statement exit can be used anywhere and always causes a jump back to the command level.