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

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

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

添加新评论

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

相关文章

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信息,这时还多了一点,虚构了浏览器信息,让服务器以为是正常的浏览器发起的请求,这样可以绕过简单的反爬虫策略...
阅读全文>>
终于用上了专业版的PyCharm含激活方法链接[Python]

终于用上了专业版的PyCharm含激活方法链接[Python]

就直接上图片吧: 激活方法链接:https://www.cnblogs.com/pupilheart/p/9734124.html
阅读全文>>
邮件发送失败小结Deferred: Connection timed out with[Port 25]

邮件发送失败小结Deferred: Connection timed out with[Port 25]

一直在忙,尽管没有忙出什么成果,也不知道忙了啥。 周末了,终于有时间看了一下自己的小服务器,发现邮件提醒好久没有发出来了. 赶紧看了一下maillog,发现: Jul 1 15:35:02 ebs-xxx sendmail[18015]: w5UK022f017326: to=<xxx...
阅读全文>>
简易中英小词典iDict v1.0发布[Github开源]

简易中英小词典iDict v1.0发布[Github开源]

用wpf写了一个小词典,方便自己使用。 功能: 简易查词 最小化系统托盘 设置开机启动 本站下载 Github下载 Github 源码 词典是用python爬的iciba词库,基本够用了。 下面是python抓取词典源码,现学现用。python上手就是简单。 # -*- codin...
阅读全文>>