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条评论
添加新评论
温馨提醒:如果您是第一次在本站留言,需要审核后才能显示哦!
that's really good stuff. thanks.
thank you.
very good.