HP 3000 Manuals

FOR Statement [ SPL to HP C/XL Migration Guide ] MPE/iX 5.0 Documentation


SPL to HP C/XL Migration Guide

FOR Statement 

          Table 6-7.  FOR Statement 

------------------------------------------------------------------------------------------------------
|                                             |                                                      |
|                     SPL                     |                  HP C/XL Equivalent                  |
|                                             |                                                      |
------------------------------------------------------------------------------------------------------
|                                             |                                                      |
| for-statement:                              | for-statement:                                       |
|                                             |                                                      |
|      1. FOR test-var := init-val            |      1. for ( test-var = init-val ;                  |
|             UNTIL end-val                   |                   test-var <= end-val ;              |
|             DO loop-statement               |                   test-var++ )                       |
|                                             |                       loop-statement                 |
|                                             |                                                      |
|      2. FOR test-var := init-val            |      2a. for ( test-var = init-val ;                 |
|             STEP step-val                   |                    test-var <= end-val ;             |
|             UNTIL end-val                   |                    test-var += step-val )            |
|             DO loop-statement               |                        loop-statement                |
|                                             |                                                      |
|                                             |      2b. for ( test-var = init-val ;                 |
|                                             |                    test-var >= end-val ;             |
|                                             |                    test-var += step-val )            |
|                                             |                        loop-statement                |
|                                             |                                                      |
|      3. FOR * test-var := init-val          |      3. for ( flag = 1 , test-var = init-val ;       |
|             UNTIL end-val                   |                   flag || test-var <= end-val ;      |
|             DO loop-statement               |                   flag = 0 , test-var++ )            |
|                                             |                       loop-statement                 |
|                                             |                                                      |
|      4. FOR * test-var := init-val          |      4a. for ( flag = 1 , test-var = init-val ;      |
|             STEP step-val                   |                    flag || test-var <= end-val ;     |
|             UNTIL end-val                   |                    flag = 0 , test-var += step-val ) |
|             DO loop-statement               |                        loop-statement                |
|                                             |                                                      |
|                                             |      4b. for ( flag = 1 , test-var = init-val ;      |
|                                             |                    flag || test-var >= end-val ;     |
|                                             |                    flag = 0 , test-var += step-val ) |
|                                             |                        loop-statement                |
|                                             |                                                      |
|                                             |                                                      |
|                                             | The generic syntax is:                               |
|                                             |                                                      |
|                                             |      for ( init-expr ;                               |
|                                             |                test-expr ;                           |
|                                             |                incr-expr )                           |
|                                             |                    statement                         |
|                                             |                                                      |
------------------------------------------------------------------------------------------------------
|                                                                                                    |
|                                             Continued                                              |
|                                                                                                    |
------------------------------------------------------------------------------------------------------

          FOR Statement(cont.) 

---------------------------------------------------------------------------------------------
|                                             |                                             |
|                     SPL                     |             HP C/XL Equivalent              |
|                                             |                                             |
---------------------------------------------------------------------------------------------
|                                             |                                             |
| init-val, step-val, and end-val are         | In general, the HP C/XL expressions are     |
| evaluated and stored.  test-val is set to   | independent, and may even be omitted!  The  |
| init-val.                                   | values in the expressions may be changed in |
|                                             | the body of the for statement.              |
| test-val is compared to end-val.  If        |                                             |
| step-val is positive or omitted, and        | The first expression (e.g., init-expr) is   |
| test-val is less than or equal to end-val,  | evaluated only once, on initial entry into  |
| loop-statement is executed.  If step-val is | the for statement.                          |
| negative, and test-val is greater than or   |                                             |
| equal to end-val, loop-statement is         | For each iteration, the second expression   |
| executed.  If the test fails, the for       | (e.g., test-expr) is evaluated.  If it is   |
| statement terminates.                       | false, the for statement is terminated.  If |
|                                             | it is true, the loop-statement is executed, |
| After loop-statement is executed, test-val  | the third expression (e.g., incr-expr) is   |
| is incremented by step-val or 1, and it is  | evaluated, and the for statement iterates.  |
| compared with end-val as above.             |                                             |
|                                             |                                             |
|                                             | In the simplest case, init-expr initializes |
|                                             | a test-var, test-expr tests it, and         |
|                                             | incr-expr increments it.                    |
|                                             |                                             |
---------------------------------------------------------------------------------------------
|                                             |                                             |
|                                             | Formats 2a and 4a deal with the case where  |
|                                             | step-val is positive.  Formats 2b and 4b    |
|                                             | handle the case where step-val is negative. |
|                                             | There is no easy way to combine the         |
|                                             | formats.                                    |
|                                             |                                             |
---------------------------------------------------------------------------------------------

In HP C/XL, the three expressions can actually contain multiple
expressions, separated by commas.  The last or right-most becomes the
value of the expression.  This is the method used to solve the SPL "*"
alternative, in formats 3 and 4.  An arbitrary variable, flag is set to
1.  Since flag is true on the first pass, it forces the execution of
loop-statement.  On subsequent passes, it is 0 or false, so the normal
end testing takes over.

          Table 6-8.  FOR Statement Examples 

---------------------------------------------------------------------------------------------
|                                             |                                             |
|                     SPL                     |             HP C/XL Equivalent              |
|                                             |                                             |
---------------------------------------------------------------------------------------------
|                                             |                                             |
|                                             |                                             |
|      FOR I:=ABC STEP 1 UNTIL 99             |      for ( I = ABC ; I <= 99 ; I++ )        |
|         DO A(I):=B(I)-X;                    |         A[I] = B[I]-X;                      |
|                                             |                                             |
|                                             |                                             |
|      FOR * I:=ABC STEP -1 UNTIL 0           |      for ( ONCE = 1 , I = ABC ;             |
|         DO A(I):=B(I)-X;                    |                ONCE || I >= 0 ;             |
|                                             |                ONCE=0 , I-- )               |
|                                             |                   A[I] = B[I]-X;            |
|                                             |                                             |
---------------------------------------------------------------------------------------------

The SPL FOR * construct may also be easily emulated by an HP C/XL
do-while statement, as illustrated by the following statements:

     I = ABC ;
     do A[I] = B[I]-X ; while (--I >= 0);



MPE/iX 5.0 Documentation