14.03.2014 Views

Scripting Guide - SAS

Scripting Guide - SAS

Scripting Guide - SAS

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Chapter 5 JSL Building Blocks 83<br />

Iterate<br />

); Ends the loop.<br />

Infinite Loops<br />

For loops that always evaluate as true create an infinite loop, which never stops. To end the loop as the<br />

script runs, press ESC on Windows (or COMMAND-PERIOD on Macintosh).<br />

Comparing For Loops in JSL to C and C++<br />

The JSL For() loop works just like it does in the C (and C++) programming language, although the<br />

punctuation is different.<br />

Tip: If you know C, watch out for the difference between JSL and C in the use of semicolons and commas.<br />

In JSL, For() is a function where commas separate arguments and semicolons join expressions. In C, for is<br />

a special clause where semicolons separate arguments and commas join them.<br />

While<br />

A related function is While(), which repeatedly tests the condition and evaluates its body script as long as<br />

the condition is true. The syntax is:<br />

While(condition, body);<br />

For example, here are two different programs that use a While() loop to find the least power of 2 that is<br />

greater than or equal to x (287). The result of both programs is 512.<br />

x = 287;<br />

// loop 1:<br />

y = 1;<br />

While(y < x, y *= 2);<br />

Show(y);<br />

// loop 2:<br />

k = 0;<br />

While(2 ^ k < x, k++);<br />

Show(2 ^ k);<br />

The scripts work like this:<br />

x = 287; Sets x to 287.<br />

// loop 1<br />

y = 1; Sets y to 1.<br />

While(<br />

y < x,<br />

Begins the While() loop.<br />

As long as y is less than x, continues evaluating the loop.

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

Saved successfully!

Ooh no, something went wrong!