11.07.2015 Views

Caché ObjectScript Reference - InterSystems Documentation

Caché ObjectScript Reference - InterSystems Documentation

Caché ObjectScript Reference - InterSystems Documentation

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Legacy Commands and FunctionsExamplesArgumentless FORIn the following example, demonstrating argumentless FOR, the user is prompted repeatedlyfor a number that is then passed to the Calc subroutine by the DO command. The FOR loopterminates when the user enters a null string (presses ENTER without inputting a number),which causes the QUIT command to execute. The argumentless FOR keyword must be followedby two (or more) spaces.FOR READ !,"Number: ",num QUIT:num="" DO Calc(num)Using FOR variable=exprWhen you specify variable=expr, <strong>Caché</strong> executes the FOR command(s) once. The value inexpr can be a literal or any valid expression. If you specify an expression, it must evaluateto a single numeric value.In the following example, the WRITE command on the line with the FOR command executesonce, with num having the value 4. It writes the number 12:SET val=4FOR num=val WRITE num*3 QUITUsing FOR variable=start:increment:endThe arguments start, increment, and end specify a start, increment, and end value, respectively.All three are evaluated as numbers. They can be integer or real, positive or negative. If yousupply string values, they are converted to their numeric equivalents at the start of the loop.When <strong>Caché</strong> first enters the loop, it assigns the start value to variable and compares thevariable value to the end value. If the variable value is less than the end value (or greaterthan it, in the case of a negative increment value), <strong>Caché</strong> executes the loop commands. Itthen updates the variable value using the increment value. (The variable value is decrementedif a negative increment is used.)Execution of the loop continues until the incrementing of the variable value would exceed(not just equal) the end value (or until <strong>Caché</strong> encounters a QUIT or GOTO). At that point,to prevent variable from exceeding end, <strong>Caché</strong> suppresses variable assignment and loopexecution ends. If the increment causes the variable value to equal the end value, <strong>Caché</strong>executes the FOR loop one last time and then terminates the loop.The following code executes the WRITE command repetitively to output, in sequence, allof the characters in string1, except for the last one. Even though the end value is specified as748 <strong>Caché</strong> <strong>ObjectScript</strong> <strong>Reference</strong>

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

Saved successfully!

Ooh no, something went wrong!