Tuesday 30 July 2013

JULY 30


Studied dynamic assignment and its example program, and a brief introduction of control statements, beginning with If-else statements..

Dynamic Assignment

field-symbol is a pointer which can be dynamically assign to a field. After assignment, we can use the field-symbol anywhere in our program in place of the actual field name.  the field-symbol statementis used to define a field-symbol, and  assign to assign a field to it. The field-symbol name must begin and end with angle brackets. 
A simple example program to explain it is given as under.
Example:
 Field-Symbol Is a Reference to Another Field
1  report ztx0915.
2  data f1(3) value 'ABC'.
3  field-symbols <f>.
4  assign f1 to <f>.   "<f> can now be used in place of f1
5  write <f>.          "writes the contents of f1
6  <f> = 'XYZ'.        "assigns a new value to f1
7  write / f1.
Output:
ABC 
XYZ
Description:
  • Line 3 defines a field-symbol named <f>.
  • Line 4 assigns the field f1 to the field-symbol <f><f> now points to the storage for variable f1, and can be used in place of f1 anywhere in the program.
  • Line 5 writes out <f>, causing the contents of f1 to be written.
  • Line 6 modifies the contents of <f>, but actually modifies f1.
  • Line 7 writes out the modified contents of f1.

Common Control Statements

  •  if- elseif Statements
  •  case Statement
  •  exit Statement
  •  do Statement
  •  while Statement
  •  continue statement
  •  check statement

Using the if-elseif Statement

The if statement in ABAP/4 has relational operators for equality and inequality and special relational operators for string comparisons and for bit masks.

Syntax for the if Statement

The following is the syntax for the if statement.
if [not] exp [ and [not] exp ] [ or [not] exp ].
---
[elseif exp.
---]
[else.
---]
endif.
where:
  • exp is a logical expression that evaluates to a true or false condition.
  • --- represents any number of lines of code. Even zero lines are allowed.
The following points apply:
  • Every if must have a matching endif.
  • else and elseif are optional.
  • Parentheses can be used. Each parentheses must be separated by a space. For example, if ( f1 = f2 ) or ( f1 = f3 ) is correct, and if (f1 = f2) or (f1 = f3) is incorrect.
  • Variables can be compared with blanks or zeros using the addition is initial. For example, if f1 is initial will be true if f1 is type c and is blank. If f1 is any other data type, the statement is true if f1contains zeros.
  • To accomplish negation, not must precede the logical expression. For example, if not f1 is initial is correct. if f1 is not initial is incorrect.
  • Variables can be compared with nulls using the addition is null. For example, if f1 is null.
 The logical operators for operands of any type are listed in Table below.

  Common Comparisons and Their Alternate Forms

Comparison
Alternate
Forms
True When
v1 = v2EQv1 equals v2
v1 <> v2NE><v1 does not equal v2
v1 > v2GTv1 is greater than v2
v1 < v2LTv1 is less than v2
v1 >= v2GE=>v1 is greater than or equal tov2
v1 <= v2LE=<v1 is less than or equal to v2
v1 between v2 andv3v1 lies between v2 and v3(inclusive)
not v1 between v2and v3v1 lies outside of the range v2to v3 (inclusive)

In the above table,v1 and v2 can be variables, literals, or field strings. In the case of variables or literals, automatic conversion is performed if the data type or length does not match. Field strings are treated as type c variables.
Comparing two values that do not have the same data type will result in an internal automatic type adjustment of one or both of the values. One type will take precedence and will dictate what type of conversion is performed.
The order of precedence is:
  • If one field is type f, the other is converted to type f.
  • If one field is type p, the other is converted to type p.
  • If one field is type i, the other is converted to type i.
  • If one field is type d, the other is converted to type d. Types c and n, however, are not converted. They are compared directly.
  • If one field is type t, the other is converted to type t. Types c and n, however, are not converted. They are compared directly.
  • If one field is type nboth are converted to type p (at this point, the other field can only be type c or x).
  • At this point, one field will be type c and the other will be type xx is converted to type c.
Example:
An Example of Using IF in the Program
1  report ztx1001.
2  data: begin of s1,
3            x value 'X',
4            y value 'Y',
5            z value 'Z',
6            end of s1,
7        begin of s2,
8            x value 'X',
9            z value 'Z',
10           end of s2.
11
12 if s1-x = s2-x.
13     write: / s1-x, '=', s2-x.
14 else.
15     write: / s1-x, '<>', s2-x.
16     endif.
17
18 if s1-x between s2-x and s2-z.
19     write: / s1-X, 'is between', s2-x, 'and', s2-z.
20 else.
21     write: / s1-X, 'is not between', s2-x, 'and', s2-z.
22     endif.
23
24 if s1 = s2.        "comparing field strings byte by byte
25     write: / 's1 = s2'.
26 else.
27     write: / 's1 <> s2'.
28     endif.
29
30 if 0 = ' '.        "Watch out for this one
31     write: / '0 = '' '''.
32 else.
33     write: / '0 <> '' '''.
34     endif.
Output:
X = X
X is between X and Z
s1 <> s2
0 = ' '
Description:
  • On line 12, s1-x is compared with s2-x. Both are type c, length 1. No conversion is performed and they are equal.
  • Line 18 is similar, but uses the between operator. The value X lies in the range X to Z, and so the test proves true.
  • Line 24 compares field strings s1 and s2 as if they were type c variables. The value of s1 is therefore XYZ, and the value of s2 is XZ. They are not equal.
  • On line 30, the literal 0 is compared with a space. The zero is stored internally as type i, the other as type c. According to the order of precedence, type c is converted to type i. Converting a blank to an integer results in a zero value, and the comparison proves to be unexpectedly true.