LEAVE和ITER (Iterate)用法区别[rpgle]

时间:2016-09-22 ┊ 阅读:22,897 次 ┊ 标签: 开发 , 编程 , 经验

先看LEAVE (Leave a Do/For Group)的用法:
The LEAVE operation transfers control from within a DO or FOR group to the
statement following the ENDDO or ENDFOR operation.
You can use LEAVE within a DO, DOU, DOUxx, DOW, DOWxx, or FOR loop to
transfer control immediately from the innermost loop to the statement following
the innermost loop's ENDDO or ENDFOR operation. Using LEAVE to leave a DO
or FOR group does not increment the index.
In nested loops, LEAVE causes control to transfer “outwards” by one level only.
LEAVE is not allowed outside a DO or FOR group.
英文技术参考介绍看起来挺多,简单来说就是完全退出循环,直接跳到循环后面第一行可执行语句执行。

示例代码:

*...1....+....2....+....3....+....4....+....5....+....6....+....7....+....
CL0N01Factor1+++++++Opcode(E)+Factor2+++++++Result++++++++Len++D+HiLoEq....
**
The following example uses an infinite loop. When the user
* types ’q’, control transfers to the LEAVE operation, which in
* turn transfers control out of the loop to the Z-ADD operation.
* 如果answer等于'q',Leave就执行后就会跳到z-add继续执行。
*
C 2 DOWNE 1
C :
C IF ANSWER = ’q’
C LEAVE
C ENDIF
C :
C ENDDO
C Z-ADD A B
**
The following example uses a DOUxx loop containing a DOWxx.
* The IF statement checks indicator 1. If it is ON, indicator
* 99 is turned ON, control passes to the LEAVE operation and
* out of the inner DOWxx loop.
**
A second LEAVE instruction is then executed because indicator 99
* is ON, which in turn transfers control out of the DOUxx loop.
*
C :
C FLDA DOUEQ FLDB
C NUM DOWLT 10
C *IN01 IFEQ *ON
C SETON 99
C LEAVE
C :
C ENDIF
C ENDDO
C 99 LEAVE
C :
C ENDDO
C :

再看Iter的用法:
The ITER operation transfers control from within a DO or FOR group to the
ENDDO or ENDFOR statement of the group. It can be used in DO, DOU, DOUxx,
DOW, DOWxx, and FOR loops to transfer control immediately to a loop's ENDDO
or ENDFOR statement. It causes the next iteration of the loop to be executed
immediately. ITER affects the innermost loop.
If conditioning indicators are present on the ENDDO or ENDFOR statement to
which control is passed, and the condition is not satisfied, processing continues
with the statement following the ENDDO or ENDFOR operation.
简单来说就是退出正在进行的循环,直接执行下一次循环(如果循环条件还满足的情况下)。

代码:

*...1....+....2....+....3....+....4....+....5....+....6....+....7....+....
CL0N01Factor1+++++++Opcode(E)+Factor2+++++++Result++++++++Len++D+HiLoEq....
**
The following example uses a DOU loop containing a DOW loop.
* The IF statement checks indicator 01. If indicator 01 is ON,
* the LEAVE operation is executed, transferring control out of
* the innermost DOW loop to the Z-ADD instruction. If indicator
* 01 is not ON, subroutine PROC1 is processed. Then indicator
* 12 is checked. If it is OFF, ITER transfers control to the
* innermost ENDDO and the condition on the DOW is evaluated
* again. If indicator 12 is ON, subroutine PROC2 is processed.
* Iter后,会跳到Endxx然后计算循环条件是否满足,继而决定继续循环或者退出循环。
CC
DOU FLDA = FLDB
C :
C NUM DOWLT 10
C IF *IN01
C LEAVE
C ENDIF
C EXSR PROC1
C *IN12 IFEQ *OFF
C ITER
C ENDIF
C EXSR PROC2
C ENDDO
C Z-ADD 20 RSLT 2 0
C :
C ENDDO
C :
*...1....+....2....+....3....+....4....+....5....+....6....+....7....+....
CL0N01Factor1+++++++Opcode(E)+Factor2+++++++Result++++++++Len++D+HiLoEq....
**
The following example uses a DOU loop containing a DOW loop.
* The IF statement checks indicator 1. If indicator 1 is ON, the
* MOVE operation is executed, followed by the LEAVE operation,
* transferring control from the innermost DOW loop to the Z-ADD
* instruction. If indicator 1 is not ON, ITER transfers control
* to the innermost ENDDO and the condition on the DOW is
* evaluated again.
C :
C FLDA DOUEQ FLDB
C :
C NUM DOWLT 10
C *IN01 IFEQ *ON
C MOVE ’UPDATE’ FIELD 20
C LEAVE
C ELSE
C ITER
C ENDIF
C ENDDO
C Z-ADD 20 RSLT 2 0
C :

看看rpgle的free格式代码:

*..1....+....2....+....3....+....4....+....5....+....6....+....7...+....
/free
    // Example 1
    // Compute n!
    factorial = 1;
    for i = 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 i = %len (field) downto 1;
        if %subst(field: i: 1) <> ’ ’;
            leave;
        endif;
    endfor;
    // Example 3
    // Extract all blank-delimited words from a sentence.
    WordCnt = 0;
    for i = 1 by WordIncr to %len (Sentence);
    // Is there a blank?
        if %subst(Sentence: i: 1) = ’ ’;
            WordIncr = 1;
            iter;
        endif;
    // We’ve found a word - determine its length:
    for j = i+1 to %len(Sentence);
        if %subst (Sentence: j: 1) = ’ ’;
            leave;
        endif;
    endfor;
    // Store the word:
    WordIncr = j - i;
    WordCnt = WordCnt + 1;
    Word (WordCnt) = %subst (Sentence: i: WordIncr);
    endfor;
/end-free

文章评论

已有3条评论

  1. Mark Mark

    that's really good stuff. thanks.

  2. very good.

添加新评论

温馨提醒:如果您是第一次在本站留言,需要审核后才能显示哦!

相关文章

Mac电脑一直显示有未读信息怎么解决?

Mac电脑一直显示有未读信息怎么解决?

上次还是macOS14,一直没有解决。索性放着不管了,反正把所有没用的信息都清理了,已经确认没有未读信息了。 最近升级了macOS15最新版本,但还是提示有6条未读信息,百思不得其姐! 查了一下网上不少人遇到此问题,有说有bug,有说点掉过滤未知联系人信息啥,都不好用。 最后查到了Apple社...
阅读全文>>
Whatsapp只能查看最近三天的聊天记录了怎么办?

Whatsapp只能查看最近三天的聊天记录了怎么办?

今天突然发现电脑上的WhatsApp只能查看最近3天的聊天记录了,显示要查看3天前的记录请到手机上查看。很是吃惊,以前一直是所有记录都自动同步到电脑版的啊,不论换电脑了还是换手机了,聊天记录从来没丢过。 查询一翻也没有找出来原因,怎么也同步不过去。 电脑版app重新安装了,也不行。 有个方法说...
阅读全文>>
ValueError: Error getting directory

ValueError: Error getting directory

ssl证书过期了 明明自动renew的 然后看log已经好久没更新成功了 查半天是当前server不信任远程连接 只能自己改代码 报错: 21-12-17 13:01:11 Generate CSR...amkevin.csr amkevin.csr generated. /home/www...
阅读全文>>
网页路径中的'.'和'..'还有'./'和'../'区别

网页路径中的'.'和'..'还有'./'和'../'区别

. 表示当前目录 .. 表示当前目录的上一级目录。 ./表示当前目录下的某个文件或文件夹,视后面跟着的名字而定 ../表示当前目录上一级目录的文件或文件夹,视后面跟着的名字而定。 例如: 文件夹 a 下面有 文件夹b c 和文件 d。 文件夹b下面有e.php 和文件f。 则e中的 . 表示...
阅读全文>>
Introduction to ILE RPG Activation Groups

Introduction to ILE RPG Activation Groups

Learn how activation groups can help your ILE RPG programs run more efficiently, how to specify the type of group to use, and closing and reclaimin...
阅读全文>>