SAP ABAP - WHILE迴圈



WHILE 迴圈語句會重複執行目標語句,直到給定條件為真。

WHILE 命令的通用格式如下:

WHILE <logical expression>  

<statement block>. 
    
ENDWHILE.

語句塊可以是單個語句或語句塊。

WHILE 迴圈執行 WHILE 和 ENDWHILE 命令包含的語句,直到邏輯表示式變為假。

流程圖

While Loop

在考慮程式效能時,WHILE 命令是首選。迴圈將持續進行,直到發現邏輯語句為假,如果發現假語句,則退出迴圈,並執行 WHILE 迴圈後的第一個語句。

示例

REPORT YS_SEP_15.
  
DATA: a type i. 
 
a = 0.
  
WHILE a <> 8.
  
   Write: / 'This is the line:', a.  
   a = a + 1.
   
ENDWHILE.

以上程式碼產生以下輸出:

This is the line: 0 
This is the line: 1 
This is the line: 2 
This is the line: 3 
This is the line: 4 
This is the line: 5 
This is the line: 6 
This is the line: 7
sap_abap_loop_control.htm
廣告

© . All rights reserved.