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.

FORMainloopFOR {READ !,"Number: ",numQUIT:num=""DO Calc(num)}Calc(a)WRITE !,"The number squared is ",a*aQUITUsing FOR variable=exprWhen you specify variable=expr, <strong>Caché</strong> executes the FOR loop once. The value in expr canbe a literal or any valid expression. If you specify an expression, it must evaluate to a singlenumeric value.In the following example, the WRITE command within the curly braces of the FOR commandexecutes once, with num having the value 4. It writes the number 12:LoopSET 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 exceedthe end value (or until <strong>Caché</strong> encounters a QUIT or GOTO). At that point, to prevent variablefrom exceeding end, <strong>Caché</strong> suppresses variable assignment and loop execution ends. If theincrement causes the variable value to equal the end value, <strong>Caché</strong> executes the FOR loopone 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 character. Because the end value is specifiedas len-1, the last character is not output. This is because the test is performed at the top of theloop, and the loop is terminated when the variable value (index) exceeds (not just matches)the end value (len-1).<strong>Caché</strong> <strong>ObjectScript</strong> <strong>Reference</strong> 41

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

Saved successfully!

Ooh no, something went wrong!