SED navigation bar go to SED home page go to Dataplot home page go to NIST home page SED Home Page SED Staff SED Projects SED Products and Publications Search SED Pages
Dataplot Vol 2 Vol 1

LOOP

Name:
    LOOP
Type:
    Support Command
Purpose:
    Execute a sequential loop.
Description:
    A sequential loop is one that has a defined start and stop value and a constant increment. The values for the start, increment, and stop can have real values (i.e., Dataplot is not limited to integer loops). Dataplot loops can have either a positive or a negative increment.

    The loop is terminated with and END OF LOOP (or END LOOP) command.

Syntax:
    LOOP FOR <par> = <start> <inc> <stop>
    where <par> is a parameter that specifies the loop index variable;
                <start> is a number or parameter that is the value for <par> on the first iteration of the loop;
                <inc> is a number or parameter that <par> is incremented by after each iteration is completed;
                <stop> is a number or parameter that determines when the loop is terminated (i.e., when <par> exceeds this value, no more iterations are performed).
Examples:
    LOOP FOR K = 1 1 100
    LOOP FOR K = START INC STOP
    LOOP FOR K = 10 -2 1
Note:
    The stop condition is tested at the end of the loop. This means all loops are executed at least once even if <stop> is less than <start>.
Note:
    Loops can be nested up to 20 levels. A unique index variable should be used for each loop.
Note:
    A maximum of 20,000 commands can be contained in a loop. Each command in the LOOP can have a maximum of 255 characters.
Note:
    IF blocks can be nested inside of a loop and a loop can be nested inside an IF block.
Note:
    Although loops can be used for data manipulations, it is more efficient to do this without loops when possible. As a rule of thumb, it is usually efficient to loop over the number of variables (i.e., columns) while it is usually rather slow to loop over the number of observations (i.e., rows), particularly if N is fairly large. Dataplot's wide array of data manipulation commands combined with clever use of tag variables and the SUBSET command can often be used to avoid writing loops.
Note:
    Sometimes it is convenient to terminate a loop before the last value has been incremented. For example, a loop may be iterating a calculation until some convergence criterion is reached. The BREAK LOOP command is used for this purpose. The BREAK LOOP command is almost always contained within an IF block.

    Note that Dataplot does not support what are referred to as WHILE (or DO WHILE or REPEAT UNTIL) loops in other languages. However, The BREAK LOOP command can be used to function much like a WHILE loop. So, although the following syntax is not supported

      LOOP WHILE A > 3

    a similar result can be obtained with

     
        LOOP FOR K = 1 1 1000000
            IF A > 3
               BREAK LOOP
            END OF IF
        

    This is demonstrated in the Program 2 example.

Note:
    The command CONTINUE LOOP is used to go to the next iteration of the loop (it does not terminate the loop). That is, all commands after the CONTINUE LOOP command are ignored.

    This is demonstrated in the Program 3 example.

Default:
    None
Synonyms:
    END LOOP is a synonym for END OF LOOP
Related Commands:
    END OF LOOP = Terminate a loop.
    IF = Conditionally execute commands.
Applications:
    Program Control
Implementation Date:
    Pre-1987
    1994/02: Added BREAK LOOP command
    2018/10: Added CONTINUE LOOP command
Program 1:
     
    LET Y1 = NORMAL RANDOM NUMBER FOR I = 1 1 100
    LET Y2 = EXPONENTIAL RANDOM NUMBERS FOR I = 1 1 100
    LET Y3 = T RANDOM NUMBERS FOR I = 1 1 100
    LET Y4 = CAUCHY RANDOM NUMBERS FOR I = 1 1 100
    LET STRING T1 = NORMAL RANDOM NUMBERS
    LET STRING T2 = EXPONENTIAL RANDOM NUMBERS
    LET NU = 20
    LET STRING T3 = T RANDOM NUMBERS
    LET STRING T4 = CAUCHY RANDOM NUMBERS
    LET X = SEQUENCE 1 1 100
    MULTIPLOT 2 2
    MULTIPLOT CORNER COORDINATES 0 0 100 100
    LOOP FOR K = 1 1 4
       TITLE ^T^K
       HISTOGRAM Y^K
    END OF LOOP
    END OF MULTIPLOT
        
Program 2:
     
    FEEDBACK OFF
    .  Computes Least Absolute Deviations fit using
    .  using iteratively re-weighted least squares.
    .  The following assumes that a string F has been defined before
    .  calling this macro to define the type of fit.  E.g., 
    .           LET STRING F = FIT Y X
    WEIGHT
    ^F
    LET MAXITER = 10
    LOOP FOR K = 1 1 MAXITER
        LET RESOLD = RES
        LET MED = MEDIAN RES
        LET TEMP = ABS(RES - MED)
        LET MAD = MEDIAN TEMP
        LET S = MAD/0.6745
        LET U = RES/S
        LET TEMP = ABS(RES)
        LET C = MEDIAN TEMP
        LET TAG = ABS(Y - PRED)
        LET WT = C/TAG  SUBSET TAG > C
        LET WT = 1 SUBSET TAG <= C
        WEIGHTS WT
        ^F
        .
        LET DELTA = (RESOLD - RES)**2
        LET NUM = SUM DELTA
        LET NUM = SQRT(NUM)
        LET DELTA2 = RESOLD*RESOLD
        LET DENOM = SUM DELTA2
        LET CONV = NUM/DENOM
        IF CONV <= 0.0001 
          BREAK LOOP
        END OF IF
    END OF LOOP
        
Program 3:
     
    let icnt = 0
    loop for k = 1 1 10
        let ival = mod(k,2)
        if ival = 0
           continue loop
        end of if
        let icnt = icnt + 1
        let x(icnt) = k
        let y(icnt) = k**2
    end of loop
    print x y
        
    The following output is generated
     ------------------------------
                   X              Y
     ------------------------------
             1.00000        1.00000
             3.00000        9.00000
             5.00000       25.00000
             7.00000       49.00000
             9.00000       81.00000
        
Date created: 09/18/2024
Last updated: 09/18/2024

Please email comments on this WWW page to [email protected].