11.07.2015 Views

Caché ObjectScript Reference - InterSystems Documentation

Caché ObjectScript Reference - InterSystems Documentation

Caché ObjectScript Reference - InterSystems Documentation

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

len-2, only the last character will be skipped. This is because the loop is terminated only whenthe variable value exceeds (not just matches) the end value.SET len=$LENGTH(string1)FOR index=1:1:len-2 WRITE $EXTRACT(string1,index)Using FOR variable=start:incrementIn this form of the FOR command there is no end value; the commands following the FORmust contain a QUIT or GOTO command to terminate the loop. In many cases, the FORinvokes a DO loop, which contains the QUIT or GOTO command.The start and increment values are evaluated as numbers. They can be integer or real, positiveor negative. If string values are supplied, they are converted to their numeric equivalents atthe start of the loop. <strong>Caché</strong> evaluates the start and increment values when it begins executionof the loop. Any changes made to these values within the loop are ignored.When <strong>Caché</strong> first enters the loop, it assigns the start value to variable and executes the loopcommands. It then updates the variable value using the increment value. (The variable valueis decremented if a negative increment is used.) Execution of the loop continues until <strong>Caché</strong>encounters a QUIT or GOTO within the loop.The following example illustrates use of the FOR variable=start:increment form to computean average for a series of user-supplied numbers. The postconditional QUIT is included toterminate execution of the loop when the user enters a null string (that is, presses ENTERwithout inputting a value). When the postconditional expression (num="") tests TRUE, <strong>Caché</strong>executes the QUIT and terminates the DO loop.The loop counter (the i variable) is used to keep track of how many numbers have beenentered. i is initialized to 0 because the counter increment occurs after the user inputs anumber. <strong>Caché</strong> terminates the loop when the user enters a null. After the loop is terminated,the SET command references i (as a local variable) to calculate the average.SET sum=0FOR i=0:1 DO AverageloopSET average=sum/iAverageloopREAD !,"Number: ",numQUIT:num=""SET sum=sum+numFurther Examples of FOR with Increment SyntaxFOR (legacy version)The following example of the FOR variable=start:increment form prompts the user for anumber and then passes the number and the current value of x to the Calc subroutine invokedby the DO command. The value of x is initialized to 1 and is incremented by 1 for each exe-<strong>Caché</strong> <strong>ObjectScript</strong> <strong>Reference</strong> 749

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!