ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

downto in a for loop???

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • downto in a for loop???

    I might have know this and forgotten cause im old like dirt but Iwill post anyway
    From IBM

    The syntax of the FOR operation is as follows:

    FOR index-name { = starting-value }
    { BY increment-value }
    { TO | DOWNTO limit-value }
    { loop body }
    ENDFOR | END

    The starting-value, increment-value, and limit-value can be numeric values or expressions with zero decimal positions. The increment value, if specified, cannot be zero.

    The BY and TO (or DOWNTO) clauses can be specified in either order. Both "BY 2 TO 10" and "TO 10 BY 2" are allowed.

    In addition to the FOR operation itself, the conditioning indicators on the FOR and ENDFOR (or END) statements control the FOR group. The conditioning indicators on the FOR statement control whether or not the FOR operation begins. These indicators are checked only once, at the beginning of the for loop. The conditioning indicators on the associated END or ENDFOR statement control whether or not the FOR group is repeated another time. These indicators are checked at the end of each loop.
    PHP Code:
     /free
        
    // Example 1
        // Compute n!
         
    factorial 1;
        for 
    1 to n;
           
    factorial factorial i;
        endfor;
         
    // Example 2
        // Search for the last nonblank character in a field.
        // If the field is all blanks, "i" will be zero.
        // Otherwise, "i" will be the position of nonblank.
         
    for = %len (fielddownto 1;
           if %
    subst(fieldi1) <> ' ';
              
    leave;
           endif;
        endfor;
         
    // Example 3
        // Extract all blank-delimited words from a sentence.
         
    WordCnt 0;
        for 
    1 by WordIncr to %len (Sentence);
           
    // Is there a blank?
           
    if %subst(Sentencei1) = ' ';
              
    WordIncr 1;
              
    iter;
           endif;
            
    // We've found a word - determine its length:
           
    for i+1 to %len(Sentence);
              if %
    subst (Sentencej1) = ' ';
                 
    leave;
              endif;
           endfor;
            
    // Store the word:
           
    WordIncr i;
           
    WordCnt WordCnt 1;
           
    Word (WordCnt) = %subst (SentenceiWordIncr);
        endfor;
      /
    end-free 
    All my answers were extracted from the "Big Dummy's Guide to the As400"
    and I take no responsibility for any of them.

    www.code400.com

  • #2
    Re: downto in a for loop???

    Originally posted by jamief
    I might have know this and forgotten cause im old like dirt but Iwill post anyway
    From IBM
    In addition to the FOR operation itself, the conditioning indicators on the FOR and ENDFOR (or END) statements control the FOR group. The conditioning indicators on the FOR statement control whether or not the FOR operation begins. These indicators are checked only once, at the beginning of the for loop. The conditioning indicators on the associated END or ENDFOR statement control whether or not the FOR group is repeated another time. These indicators are checked at the end of each loop.
    Conditioning indicators on DO, DOU, DOW loops? Conditioning inds on END statements? Thanks, Jamie. Now I feel old like dirt.
    http://www.linkedin.com/in/chippermiller

    Comment

    Working...
    X