📄 ch10.htm
字号:
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<P>
<TT>F1</TT> is assigned every other component from <TT>s</TT>
beginning with <TT>s-c1</TT>, and <TT>f2</TT> is assigned every
other component beginning with <TT>s-c2</TT>.
<H3><A NAME="ModifyingValueswithinTTFONTSIZEdovaryingenddoFONTTT">
Modifying Values within <TT><FONT SIZE=4>do ... varying/enddo</FONT></TT>
</A></H3>
<P>
You can modify the value of either <TT><I>f1</I></TT>
or <TT><I>s</I></TT> within the
<TT>do ... varying/enddo</TT> loop. When the <TT>enddo</TT> statement
is executed, <I>the current value of </I><TT><I>f1</I></TT><I>
value is copied back to the component it came from, whether it
was modified or not</I>. In Listing 10.11, report <TT>ztx1011</TT>
illustrates this functionality.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 10.11 The Current Value of </B><TT><B>F1</B></TT><B>
Is Written Back to the Sending Component When </B><TT><B>ENDDO</B></TT><B>
Is Executed<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 report ztx1011.
2 data: f1 type i,
3 begin of s,
4 c1 type i value 1,
5 c2 type i value 2,
6 c3 type i value 3,
7 c4 type i value 4,
8 c5 type i value 5,
9 c6 type i value 6,
10 end of s.
11 field-symbols <f>.
12
13 write / ''.
14 do 6 times varying f1 from s-c1 next s-c2.
15 if sy-index = 6.
16 s-c6 = 99.
17 else.
18 f1 = f1 * 2.
19 endif.
20 assign component sy-index of structure s to <f>. "<f> now points to
21 write <f>. "a component of s
22 enddo.
23
24 write / ''.
25 do 6 times varying f1 from s-c1 next s-c2.
26 write f1.
27 enddo.
</PRE>
</BLOCKQUOTE>
<HR>
<P>
<IMG SRC="../button/output.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/output.gif">
<P>
The code in Listing 10.11 produces this output:
<BLOCKQUOTE>
<PRE>
1 2 3 4 5 99
2 4 6 8 10 6
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>On line 14, values are read from the components of field string
<TT>s</TT> into <TT>f1</TT> one at a time.
<LI>Line 18 modifies the value of each <TT>f1</TT>, multiplying
it by 2. The last value of <TT>f1</TT> is not modified. Instead,
the sending component is assigned the value <TT>99</TT>.
<LI>Line 20 assigns a component of <TT><I>s</I></TT>
to field-symbol <TT><f></TT>. The first time through the
loop, component 1 (<TT><I>c1</I></TT>)
is assigned to <TT><f></TT>. The second time through, component
2 (<TT><I>c2</I></TT>) is assigned,
and so on. Line 21 writes out the contents of the component that
sent its value to <TT>f1</TT>. In the output listing, you can
see the contents of each component of <TT><I>s</I></TT>
before <TT>enddo</TT> is executed.
<LI>When the <TT>enddo</TT> statement is executed on line 22,
the contents of <TT>f1</TT> replace the contents of the sending
component.
<LI>Varying through the loop again (lines 25 through 27) displays
the new contents of <TT><I>s</I></TT>.
This shows that the value of <TT>f1</TT> within the loop always
overwrites the contents of the sending component.
</UL>
<P>
An <TT>exit</TT> statement within the loop will <I>not</I> prevent
the modified contents of <TT>f1</TT> from being written back to
the sending field. The only way to leave the loop without the
contents of <TT>f1</TT> overwriting the sending field is by executing
a <TT>stop</TT> statement or an error message statement within
the loop (both covered in later chapters).
<H2><A NAME="UsingtheTTFONTSIZEwhileFONTTTFONTSIZEStatementFONT"><FONT SIZE=5 COLOR=#FF0000>
Using the <TT><FONT SIZE=5>while</FONT></TT><FONT SIZE=5>
Statement</FONT></FONT></A></H2>
<P>
The <TT>while</TT> statement is a looping mechanism similar to
<TT>do</TT>.
<H3><A NAME="SyntaxfortheTTFONTSIZEwhileFONTTTFONTSIZEStatementFONT">
Syntax for the <TT><FONT SIZE=4>while</FONT></TT><FONT SIZE=4>
Statement</FONT></A></H3>
<P>
The following is the syntax for the <TT>while</TT> statement.
<BLOCKQUOTE>
<PRE>
while <I>exp</I> [ vary <I>f1</I> from <I>s-c1</I> next <I>s-c2</I> [ vary <I>f2 </I>from <I>s2-c1</I> next <I>s2-c2</I> ... ]
---
[ exit. ]
---
endwhile.
</PRE>
</BLOCKQUOTE>
<P>
where:
<UL>
<LI><TT><I>exp</I></TT> is a logical
expression.
<LI><TT><I>s</I></TT> is a field
string having the components <TT><I>c1</I></TT>
and <TT><I>c2</I></TT>.
<LI><TT><I>f1</I></TT> is a variable.
The components of <TT><I>s</I></TT>
must be able to be converted to the data type and length of <TT><I>f1</I></TT>.
<LI><TT>...</TT> represents any number of complete <TT>vary</TT>
clauses.
<LI><TT><I>---</I></TT> represents
any number of lines of code.
</UL>
<P>
The following points apply:
<UL>
<LI><TT>while</TT> loops can be nested an infinite number of times
and also be nested within any other type of loop.
<LI><TT>exit</TT> prevents further loop processing and exits immediately
out of the current loop. Processing continues at the next executable
statement after <TT>endwhile</TT>.
<LI>Within the loop, <TT>sy-index</TT> contains the current iteration
number. After <TT>endwhile</TT>, <BR>
<TT>sy-index</TT> contains the value it had before entering the
loop. With nested <TT>while</TT> loops, <TT>sy-index</TT> contains
the iteration number of the loop in which it is used.
<LI><TT>endwhile</TT> always copies the value of <TT><I>f1</I></TT>
back into the sending component.
<LI>On a <TT>while</TT> statement containing a logical expression
and a <TT>varying</TT> addition, the logical expression is evaluated
first.
</UL>
<P>
<TT>while</TT> is very similar to <TT>do</TT>. Here, it is used
to place an equal number of dashes on either side of a string.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 10.12 An Example of the Use of the </B><TT><B>WHILE</B></TT><B>
Statement<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 report ztx1012.
2 data: l, "leading characters
3 t, "trailing characters
4 done. "done flag
5 parameters p(25) default ' Vendor Number'.
6 while done = ' ' "the expression is evaluated first
7 vary l from p+0 next p+1 "then vary assignments are performed
8 vary t from p+24 next p+23.
9 if l = ' ' and t = ' '.
10 l = t = '-'.
11 else.
12 done = 'X'.
13 endif.
14 endwhile.
15 write: / p.
</PRE>
</BLOCKQUOTE>
<HR>
<P>
<IMG SRC="../button/output.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/output.gif">
<P>
The code in Listing 10.12 produces this output:
<BLOCKQUOTE>
<PRE>
----Vendor Number ----
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Lines 2 and 3 define two single character variables <TT>l</TT>
and <TT>t</TT>. Line 3 defines a flag to indicate when processing
is complete.
<LI>Line 5 defines <TT>p</TT> as character <TT>25</TT> with a
default value.
<LI>On line 6, the expression on the <TT>while</TT> statement
is first evaluated. It proves true the first time through the
loop. The assignments on lines 7 and 8 are then performed. Line
7 assigns the first character from <TT>p</TT> to <TT>l</TT> and
line 8 assigns the last character to <TT>t</TT>.
<LI>If <TT>l</TT> and <TT>t</TT> are both blank, line 10 assigns
a dash to both. If they are not, line 12 assigns an <TT>'X'</TT>
to the <TT>done</TT> flag.
<LI>On line 14, <TT>endwhile</TT> copies the values from <TT>l</TT>
and <TT>t</TT> back to <TT>p</TT>.
<LI>The <TT>while</TT> loop repeats again from line 6 as long
as the <TT>done</TT> flag is blank.
</UL>
<H2><A NAME="UsingtheTTFONTSIZEcontinueFONTTTFONTSIZEstatementFONT"><FONT SIZE=5 COLOR=#FF0000>
Using the <TT><FONT SIZE=5>continue</FONT></TT><FONT SIZE=5>
statement</FONT></FONT></A></H2>
<P>
The <TT>continue</TT> statement is coded within a loop. It acts
like a <TT>goto</TT>, passing control immediately to the terminating
statement of the loop and beginning a new loop pass. In effect,
it causes the statements below it within the loop to be ignored
and a new loop pass to begin. The effect of the <TT>continue</TT>
statement is shown in Figure 10.1.
<P>
<A HREF="javascript:popUp('f10-1.gif')"><B>Figure 10.1 : </B><I>The continue</I><TT><I>
</I></TT><I>statement jumps to the end of the loop, ignoring
all statements after it for the current loop pass</I>.</A>
<P>
<IMG SRC="../button/output.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/output.gif">
<P>
The code in Figure 10.1 produces this output:
<BLOCKQUOTE>
<PRE>
1 2 9 10
</PRE>
</BLOCKQUOTE>
<H3><A NAME="SyntaxfortheTTFONTSIZEcontinueFONTTTFONTSIZEStatementFONT">
Syntax for the <TT><FONT SIZE=4>continue</FONT></TT><FONT SIZE=4>
Statement</FONT></A></H3>
<P>
The following is the syntax for the <TT>continue</TT> statement.
It can be used within a <TT>do</TT>, <TT>while</TT>, <TT>select</TT>,
or <TT>loop</TT>. (The <TT>loop</TT> statement is covered in the
next chapter.)
<BLOCKQUOTE>
<PRE>
[do/while/select/loop]
---
continue.
---
[enddo/endwhile/endselect/endloop]
</PRE>
</BLOCKQUOTE>
<P>
where:
<UL>
<LI><TT><I>---</I></TT> represents
any number of lines of code.
</UL>
<P>
The following points apply:
<UL>
<LI><TT>continue</TT> can only be coded within a loop.
<LI><TT>continue</TT> has no additions.
</UL>
<P>
Listing 10.13 is like a <TT>goto</TT> statement. It causes a jump
to the end of the current loop. This program removes duplicate
colon and backslash characters from an input string.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 10.13 An Example of the Use of the </B><TT><B>CONTINUE</B></TT><B>
Statement<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 report ztx1013.
2 parameters p(20) default 'c::\\\xxx\\yyy'.
3 data: c, "current character
4 n. "next character
5
6 do 19 times varying c from p+0 next p+1
7 varying n from p+1 next p+2.
8 if c na ':\'.
9 continue.
10 endif.
11 if c = n.
12 write: / 'duplicate', c, 'found', 'at position', sy-index.
13 endif.
14 enddo.
</PRE>
</BLOCKQUOTE>
<HR>
<P>
<IMG SRC="../button/output.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/output.gif">
<P>
The code in Listing 10.13 produces this output:
<BLOCKQUOTE>
<PRE>
duplicate : found at position 2
duplicate \ found at position 4
duplicate \ found at position 5
duplicate \ found at position 10
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Line 2 defines a character length <TT>20</TT> input parameter
<TT>p</TT> and assigns it a default value.
<LI>Line 6 starts a loop. At the first loop pass, the first character
of <TT>p</TT> is assigned to <TT>c</TT> and the character following
it is assigned to <TT>n</TT>.
<LI>Line 8 is true when <TT>c</TT> contains a character other
than <TT>:</TT> or <TT>/</TT>.
<LI>Line 9 jumps directly to line 14, causing the loop to repeat
using the next character.
<LI>If line 8 is not true, line 11 is executed and is true if
the next character is the same as the current character.
<LI>The <TT>enddo</TT> copies the values from <TT>c</TT> and <TT>n</TT>
back into <TT>p</TT> and the loop repeats <BR>
19 times altogether.
</UL>
<H2><A NAME="UsingtheTTFONTSIZEcheckFONTTTFONTSIZEstatementFONT"><FONT SIZE=5 COLOR=#FF0000>
Using the <TT><FONT SIZE=5>check</FONT></TT><FONT SIZE=5>
statement</FONT></FONT></A></H2>
<P>
The <TT>check</TT> statement is coded within a loop. It can act
very much like <TT>continue</TT>, passing control immediately
to the terminating statement of the loop and bypassing the statements
between. Unlike <TT>continue</TT>, it accepts a logical expression.
If the expression is true, it does nothing. If it is false, it
jumps to the end of the loop. The effect of the <TT>check</TT>
statement is shown in Figure 10.2.
<P>
<A HREF="javascript:popUp('f10-2.gif')"><B>Figure 10.2 : </B><I>The check</I><TT><I>
</I
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -