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

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

先看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.

添加新评论

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

相关文章

popup.js怎么和content.js通信?[JQuery]

popup.js怎么和content.js通信?[JQuery]

这两天为了实现一个谷歌浏览器插件功能,研究了半天怎么让插件来改特定网页里的特定字段的值,而这个值又来自popup的网页预先设定,下一步实现动态加载,可以让更多组实现便利。 目的很简单,我们有一个list,需要填到网页的某个字段,当然是好几个,这些值是设定好的,网页系统我们没法改,input想...
阅读全文>>
T480自动关机无法启动(解决办法)

T480自动关机无法启动(解决办法)

周五升级更新后,用一会就关机,还开不了,怎么按开机键也没有反应。 周六折腾一上午,拔电源,断电,20秒复位,把电池都拿下来了。都不行。 下午实在没办法了,给售后打电话让拿去检测一下。 然后拿售后去了,给检测一会没问题,说内存不干净给擦一下好了,开机正常了。 付款50块检测费,开心的拿回家,一...
阅读全文>>
office2016 产品已激活 却同时显示需要激活(解决办法)

office2016 产品已激活 却同时显示需要激活(解决办法)

直接上命令吧: c:\Program Files\Microsoft Office\Office16>cscript.exe ospp.vbs /dstatus Microsoft (R) Windows Script Host Version 5.812 版权所有(C) Microso...
阅读全文>>
Excel 2016打开xls文件显示空白的解决办法

Excel 2016打开xls文件显示空白的解决办法

问题描述: 安装Office 2016后,正版激活一直挺好的,不过今天发现,双击EXCEL旧版本文件打开后显示为空白。 上网搜索一翻,终于知道问题所在,可能和昨天的一个office更新有关。 解决办法: 开始——运行——regedit 修改HKEY_CLASSES_ROOT\Excel.She...
阅读全文>>
如何绕过登录抓取js动态加载网页数据[Python]

如何绕过登录抓取js动态加载网页数据[Python]

今天经历了一翻折腾,把一个需要登录网站并js动态加载的数据一一给抓下来了。 首先,登录时有cookie,我们需要把cookie保存下来,用urllib2构建request时加入header信息,这时还多了一点,虚构了浏览器信息,让服务器以为是正常的浏览器发起的请求,这样可以绕过简单的反爬虫策略...
阅读全文>>