Thursday 1 August 2013

AUGUST 1


Studied the use of while, continue and check statements with example programs.

Using the while Statement

The while statement is a looping mechanism similar to do.

Syntax for the while Statement

The following is the syntax for the while statement.
while exp [ vary f1 from s-c1 next s-c2 [ vary f2 from s2-c1 next s2-c2 ... ]
    ---
    [ exit. ]
    ---
    endwhile.
where:
  • exp is a logical expression.
  • s is a field string having the components c1 and c2.
  • f1 is a variable. The components of s must be able to be converted to the data type and length of f1.
  • ... represents any number of complete vary clauses.
  • --- represents any number of lines of code.
Example:
An Example of the Use of the WHILE Statement
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.
Output:  ----Vendor Number ----
Description:
  • Lines 2 and 3 define two single character variables l and t. Line 3 defines a flag to indicate when processing is complete.
  • Line 5 defines p as character 25 with a default value.
  • On line 6, the expression on the while 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 p to l and line 8 assigns the last character to t.
  • If l and t are both blank, line 10 assigns a dash to both. If they are not, line 12 assigns an 'X' to the done flag.
  • On line 14, endwhile copies the values from l and t back to p.
  • The while loop repeats again from line 6 as long as the done flag is blank.

Using the continue statement

The continue statement is coded within a loop. It acts like a goto, 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. 

Syntax for the continue Statement

The following is the syntax for the continue statement.
[do/while/select/loop]
    ---
    continue.
    ---
    [enddo/endwhile/endselect/endloop]
where:
  • --- represents any number of lines of code.
The following points apply:
  • continue can only be coded within a loop.
  • continue has no additions.
Example:
An Example of the Use of the CONTINUE Statement
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.
Output:
duplicate : found at position          2
duplicate \ found at position          4
duplicate \ found at position          5
duplicate \ found at position         10

Using the check statement

The check statement is coded within a loop. It can act very much like continue, passing control immediately to the terminating statement of the loop and bypassing the statements between. Unlike continue, 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. 

Syntax for the check Statement

[do/while/select/loop]
    ---
    check exp.
    ---
    [enddo/endwhile/endselect/endloop]
where:
  • exp is a logical expression.
  • --- represents any number of lines of code.
Example:
 Use of the CHECK Statement
1  report ztx1014.
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      check c ca ':\'.
9      if c = n.
10         write: / 'duplicate', c, 'found', 'at position', sy-index.
11         endif.
12     enddo.
Output:
duplicate : found at position          2
duplicate \ found at position          4
duplicate \ found at position          5
duplicate \ found at position 10