Wednesday 31 July 2013

JULY 31


Continued with control statements- elseif, case , exit and do statements and their examples.

Using elseif

We use elseif to avoid nesting ifs. Nesting ifs can be difficult to read and maintain.
an example below shows the construct.
Example:
Using ELSEIF Is Clearer than Using Nested IF
1  report ztx1002.
2  parameters: f1 default 'A',
3              f2 default 'B',
4              f3 default 'C'.
5
6  if      f1 = f2.   write: / f1, '=', f2.
7  elseif  f1 = f3.   write: / f1, '=', f3.
8  elseif  f2 = f3.   write: / f2, '=', f3.
9  else.              write: / 'all fields are different'.
10     endif.
11
12 *lines 5-9 do the same as lines 14-26*
13
14 if f1 = f2.
15     write: / f1, '=', f2.
16 else.
17     if  f1 = f3.
18         write: / f1, '=', f3.
19     else.
20         if  f2 = f3.
21             write: / f2, '=', f3.
22         else.
23             write: / 'all fields are different'.
24             endif.
25         endif.
26     endif.
Output:
all fields are different 
all fields are different 
Description:
  • If f1 = f2, line 6 is true.
  • If f1 = f3, line 7 is true.
  • If f2 = f3, line 8 is true.
  • If none of the above are true, line 9 is true.
  • Lines 14 through 26 do the same as lines 6 through 10.

Using the case Statement

The case statement performs a series of comparisons.

Syntax for the case Statement

The following is the syntax for the case statement.
case v1.
    when v2 [ or vn ... ].
        ---
    when v3 [ or vn ... ].
        ---
    [ when others.
        --- ]
    endcase.
where:
  • v1 or v2 can be a variable, literal, constant, or field string.
  • --- represents any number of lines of code. Even zero lines are allowed.
The following points apply:
  • Only statements following the first matching when are executed.
  • when others matches if none of the preceding whens match.
  • If when others is not coded and none of the whens match, processing continues with the first statement following endcase.
  • Expressions are not allowed.
  • Field strings are treated as type c variables.
Case is very similar to if/elseif. The only difference is that on each if/elseif, we can specify a complex expression. With case, we can specify only a single value to be compared, and values are always compared for equality.
An example is given as follows:
Example:
The CASE Statement Performs a Series of Comparisons
1  report ztx1005.
2  parameters f1 type i default 2.
3
4  case f1.
5      when 1.         write / 'f1 = 1'.
6      when 2.         write / 'f1 = 2'.
7      when 3.         write / 'f1 = 3'.
8      when others.    write / 'f1 is not 1, 2, or 3'.
9      endcase.
10
11 * The following code is equivalent to the above case statement
12 if         f1 = 1.  write / 'f1 = 1'.
13     elseif f1 = 2.  write / 'f1 = 2'.
14     elseif f1 = 3.  write / 'f1 = 3'.
15     else.           write / 'f1 is not 1, 2, or 3'.
16     endif.
Output:
f1 = 2
f1 = 2
Description:
  • Line 2 defines f1 as a single character parameter having a default value 2.
  • On line 4, the case statement begins and f1 is compared with 1, 2, and 3, in sequence.
  • Line 6 matches and executes the write statement that immediately follows it. The remaining when statements are ignored.
  • The next line executed is line 12.

Using the exit Statement

The exit statement prevents further processing from occurring.

Syntax for the exit Statement

exit.
Following is shown a sample program using exit.
Example:
sing EXIT to Stop Program Processing Dead in Its Tracks
1  report ztx1006.
2  write: / 'Hi'.
3  exit.
4  write: / 'There'.
Output: Hi Description: Exit prevents further processing, so the exit on line 3 prevents line 4 from being executed.

Using the do Statement

The do statement is a basic loop mechanism.

Syntax for the do Statement

The following is the syntax for the do statement.
do [ v1 times ] [ varying f1 from s-c1 next s-c2 [ varying f2 from s2-c1 next s2-c2 ... ] ].
    ---
    [exit.]
    ---
    enddo.
where:
  • v1 is a variable, literal, or constant.
  • 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 varying clauses.
  • --- represents any number of lines of code.
The following points apply:
  • do loops can be nested an unlimited number of times.
  • exit prevents further loop processing and exits immediately out of the current loop. It does not terminate the program when inside of a do loop. Processing continues at the next executable statement after the enddo.
  • we can create an infinite loop by coding do without any additions. In that situation, use exit within the loop to terminate loop processing.
  • Modifying the value of v1 within the loop does not affect loop processing.

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.

Monday 29 July 2013

JULY 29


Created a program for illustrating move-corresponding statements concept and studied various statements for performing calculations.
Example:
The MOVE-CORRESPONDING Statement Generates Individual MOVE Statements and Thus Performs Data Conversion
 1 report ztx0912.
 2 data: begin of s1,
 3           c1    type p decimals 2 value '1234.56',
 4           c2(3)                   value 'ABC',
 5           c3(4)                   value '1234',
 6           end of s1,
 7       begin of s2,
 8           c1(8),
 9           x2(3)                   value 'XYZ',
10           c3    type i,
11           end of s2.
12 write: / 's1                          :', s1-c1, s1-c2, s1-c3.
13 write: / 's2 before move-corresponding:', s2-c1, s2-x2, s2-c3.
14 move-corresponding s1 to s2. "same as coding the following two statements
15 * move s1-c1 to s2-c1.       "performs conversion
16 * move s1-c3 to s2-c3.       "performs conversion
17 write: / 's2 after  move-corresponding:', s2-c1, s2-x2, s2-c3.
Output:
s1                          :         1,234.56  ABC 1234
s2 before move-corresponding:          XYZ          0
s2 after  move-corresponding: 1234.56  XYZ      1,234

Performing Calculations

we can perform calculations using the following statements:
  • compute
  • add or add-corresponding
  • subtract or subtract-corresponding
  • multiply or multiply-corresponding
  • divide or divide-corresponding

Using the compute Statement:

Compute is the statement most commonly used to perform calculations.

Syntax for the compute Statement

The following is the syntax for the compute statement. Operators and operands must be separated by spaces. More than one operator per statement is permitted.
compute v3 = v1 op v2 [op vn ...].
or
v3 = v2 op v2 [op vn ...].

where:
  • v3 is the receiving variable for the result of the computation.
  • v1v2, and vn are the operands.
  • op is a mathematical operator.
 List of valid operators:
 Valid Operators for the COMPUTE Statement
Operator
Operation
+
Addition
-
Subtraction
*
Multiplication
/
Division
**
Exponentiation
DIV
Integer division
MOD
Remainder of integer division

Operator precedence is as follows:
  • Built-in functions are evaluated,
  • then exponentiation,
  • then */DIV, and MOD, in the order they appear in the expression,
  • then + and - in the order they appear in the expression.

Using the add and add-corresponding Statements

Use the add statement to add one number to another. Field strings with components having the same name can be added together with add-corresponding.

Syntax for the add Statement

Following is the syntax for the add statement. Conversions are performed as necessary in the same way as the compute statement.
add v1 to v2.
where:
  • v2 is the variable being added to.
  • v1 is the variable added to v2.
The syntax for the subtractmultiply, and divide is similar.

Syntax for the add-corresponding Statement

Below is the syntax for the add-corresponding statement.
add-corresponding s1 to s2.
where:
  • s2 is the field string being added to.
  • s1 is the field string added to s2.
An add statement is generated for each pair of components having the same name in s1 and s2. Data conversions are performed in the same way as for the add statement. Subtract-corresponding,multiply-correspondingand divide-corresponding operate in a similar fashion.
The program below explains the concept.
Example:
  Using ADDSUBTRACTMULTIPLYDIVIDE, and CORRESPONDING Statements
 1 report ztx0913.
 2 data: f1 type i value 2,
 3       f2 type i value 3,
 4       begin of s1,
 5           c1 type i value 10,
 6           c2 type i value 20,
 7           c3 type i value 30,
 8           end of s1,
 9       begin of s2,
10           c1 type i value 100,
11           x2 type i value 200,
12           c3 type i value 300,
13           end of s2.
14 add                    f1  to   f2.   write  / f2.   "f1 is unchanged
15 subtract               f1  from f2.   write  / f2.
16 multiply               f2  by   f1.   write  / f2.
17 divide                 f2  by   f1.   write  / f2.
18 add-corresponding      s1  to   s2.   write: / s2-c1, s2-x2, s2-c3.
19 subtract-corresponding s1  from s2.   write: / s2-c1, s2-x2, s2-c3.
20 multiply-corresponding s2  by   s1.   write: / s2-c1, s2-x2, s2-c3.
21 divide-corresponding   s2  by   s1.   write: / s2-c1, s2-x2, s2-c3.
Output:
     5
         3
         6
         3
       110         200         330
       100         200         300
     1,000         200       9,000
       100         200         300

Friday 26 July 2013

JULY 26


Example programs to understand and implement the concepts of move statement.

Using move with Field Strings:

With move, a field string name specified without a component name is treated as if it were a variable of type c
Example:
The MOVE Statement Treats a Field String Name without a Component Name like a Variable of Type 
 1 report ztx0908.
 2 data: f1(4) value 'ABCD',
 3       begin of s1,
 4           c1(1),
 5           c2(2),
 6           c3(1),
 7           end of s1.
 8 s1 = f1.                         "s1 is treated as a char 4 variable
 9 write: / s1,                     "writes ABCD
10        / s1-c1, s1-c2, s1-c3.    "writes A BC D
Output:
ABCD 
A BC D
Description:
  • Line 2 defines f1 as a four-byte variable of type c.
  • Lines 3 through 7 define s1 as a field string having three components: f1f2, and f3. The total length of s1 is calculated by totaling the lengths of its components: 1+2+1=4.
  • Line 8 moves the value from f1 to s1 byte by byte, as if they were both variables of type c.
  • Line 9 writes s1 out as a four-character variable.
  • Line 10 writes out the contents of the components of s1.

Field String Moves Involving Numeric Fields

If the sending field is category character (types cndt, or x) and the target is a field string containing a numeric field (types ip, or f), no data conversion is performed. The move proceeds as if both were purely character. The reverse is also true. No conversions are performed when moving a numeric to a field string containing a character field. In both cases, the results are invalid and are undefined.
Following shows a sample program that make an invalid conversion of a numeric field to a character field string.
 Example:
Moving a Numeric Field to a Character Field String Is Invalid
 1 report ztx0909.
 2 data: fc(5) type c,
 3       begin of s,
 4            fi type i,
 5            end of s.
 6
 7 fc = '1234'.
 8 s = fc.           "c<-c, no conversion performed
 9 write s-fi.       "writes junk
10
11 s-fi = 1234.      "assign a valid value
12 fc = s.           "c<-c, no conversion performed
13 write / fc.       "writes junk
14
15 s-fi = 1234.      "assign a valid value
16 fc = s-fi.        "c<-i conversion performed
17 write / fc.       "writes 1234
Output:
875770,417
"### 
1234
Description:
  • On line 2, f1 is defined as an integer having a value of 1234.
  • On lines 3 through 5, s1 is defined as a single component c1 type c length 12.
  • On line 6, f1 is moved to s1. No conversion is performed because s1 is a field string, so it is treated like type c.
  • On line 7, s1-c1 is written out and the results are garbage.
  • On line 8, s1-c1 is assigned a valid character string '1234'.
  • On line 9, s1 is moved to f1. No conversion is performed.
  • Line 10 writes out f1, and again, the results are garbage.
  • On Line 11, f1 is assigned a valid value of 1234.
  • On line 12, f1 is assigned to s1-c1. Because the assignment is to the component and not to the field string, conversion is performed and line 13 writes out a valid value.

Moving from One Field String to Another Using Character Data Types

 We can use the move statement on two field strings if both strings contain components of only character data types (cndt, and x). The following program illustrates this concept.
Example-1:
Using MOVE with Two Field Strings Composed Entirely of Valid Character Data Types
 1 report ztx0910.
 2 data: begin of s1,
 3           d1     type d value '19980217',   "8 bytes
 4           n1(4)  type n value '1234',       "4 bytes
 5           c1            value 'A',          "1 byte
 6           c2            value 'B',          "1 byte
 7           end of s1,
 8       begin of s2,
 9           y1(4)  type n,                    "4 bytes
10           m1(2)  type c,                    "2 bytes
11           d1(2)  type n,                    "2 bytes
12           n1(2)  type c,                    "2 bytes
13           c1(4)  type c,                    "4 bytes
14           end of s2.
15  s2 = s1.
16  write: / s1,
17         / s2,
18         / s1-d1, s1-n1, s1-c1, s1-c2,
19         / s2-y1, s2-m1, s2-d1, s2-n1, s2-c1.
Output:
199802171234AB
199802171234AB
19980217 1234 A B 
1998 02 17 12 34AB
Description:
  • Lines 2 through 14 define two field strings composed entirely of character type fields. Each field string is 14 bytes long in total.
  • On line 15, s1 is moved to s2, treating each field string as if it is a single type c field 14 bytes long.
  • On lines 16 and 17, both are written out as character fields.
  • On lines 18 and 19, the components of each field string are written out. s1-d1 has been split across s2-y1m1, and d1. The first two bytes of s1-n1 went into s2-n1. The remaining bytes from s1 went to s2-c1.

Moving from One Field String to Another with Numeric Data Types

Most operating systems require the machine address of numeric fields to conform to specific rules. For example, the address of a four-byte integer might be required to be divisible by two. That rule is often stated as "a four-byte integer must be aligned on an even byte boundary." Thus, the rule to which the address of a field conforms is called the alignment.
In order to satisfy alignment requirements, a typical compiler will insert padding bytes before a field that requires alignment. For example, if our four-byte integer begins at offset 0003 from the beginning of a program, a padding byte is necessary to align it on a two-byte boundary. The compiler will place an unused byte at offset 0003 so that the integer begins at offset 0004, causing it to be properly aligned. Padding bytes are invisible to the programmer, but their effects can be seen at times.
If we create a field string having a mixture of character and numeric fields, padding bytes are sometimes inserted by the system to bring numeric fields into alignment. If we attempt to use a move statement on such a field string to move its contents to another, these padding bytes can cause unexpected results.
Therefore, we can only use move if one of the following is true:
  • Both field strings consist entirely of character fields (types cndt, and x).
  • The data types, lengths, and position of all components in both field strings match exactly (the names of the components do not have to match).
If both the sending and receiving fields are entirely composed of character fields, no padding bytes will exist and the result of using move is predictable. If there is a mixture of character and numeric types, padding bytes might exist and the result of using move can be unexpected.
An example is shown below:
Example:
Padding Bytes Can Cause Unexpected Results When Moving Data Between Field Strings
 1 report ztx0911.
 2 data: begin of s1,
 3           c1 value 'A',             "one byte

 4           c2 type i value 1234,     "four bytes, usually needs padding
 5           c3 value 'B',             "one byte
 6           end of s1,

 7       begin of s2,
 8           c1,                       "one byte
 9           c2(4),                    "four bytes, no padding
10           c3,                       "one byte
11           end of s2,
12       begin of s3,                  "s3 matches s1 exactly:
13           x1,                       "- data types the same
14           x2 type i,                "- number of fields the same
15           x3,                       "- fields in the same order
16           end of s3.                "(names don't have to match)
17 s2 = s1.
18 write: / s2-c1, s2-c2, s2-c3.
19 s3 = s1. 
20 write: / s3-x1, s3-x2, s3-x3
Output:
A ###" # 
A 1,234 B.
Description:
s1-c1 is only one byte long, so c2 has an uneven offset from the beginning of the field string and requires padding on most systems, which makes s1 longer than s2. When s1 is assigned to s2c3 does not line up and some of s1-c2 ends up at the beginning of s2-c3. However, s3 matches s1 exactly, so the move on line 14 works perfectly.
.

Thursday 25 July 2013

JULY 25


Studied Data conversion during move statements and effect on length adjustment and the various rules of conversion with an example program.

Data Conversions:

If two variables have different data types or lengths, the data is converted when it is moved. This is called an automatic adjustment. If the lengths of the sending and receiving variables do not match, an automatic lengthadjustment is performed. If the data types do not match, an automatic type adjustment is performed.
If the data types of the sending and receiving fields are the same but the lengths differ, a length adjustment is performed . The effect is shown in the table below where the sending field is the "From" field.

Effect of Length Adjustment Varies with the Data Type

Type
When assigning to a longer field, the 'from' value is:When assigning to a shorter field, the 'from' value is:
c
Right-padded with blanksRight-truncated
x
Right-padded with zerosRight-truncated
n
Left-padded with zerosLeft-truncated
p
Left-padded with zerosAssigned if the numeric value will fit in the 'to' field.
 
If the numeric value is too large for the receiving field, a short dump occurs.

The remaining data types (fid, and t) are all of a fixed length, so the sending and receiving fields will always be the same length if they are of the same data type.
Points to notice are:
  • The peculiar compression performed for type c to n.
  • The capability to assign invalid values to types d and t.
  • The odd treatment of invalid characters during conversion of type c to x.
  • The unexpected usage of the reserved sign byte in p to c conversions.
  • The use of * to indicate overflow in p to c conversions.
  • An entirely blank c field is converted to a p field having a zero value.
Rules for type adjustments are provided in Table below. The conversion rules for type i are the same as for type p. Included are conversions with unusual behaviors.
Rules for Type Adjustments

From Type
To Type
Conversion Rules
c
p
The sending field can only contain numbers, a single decimal point, and an optional sign. The sign can be leading or trailing. Blanks can appear on either side of the value. It is right-justified and padded on the left with zeros. An entirely blank sending field is converted to zero.
c
d
The sending field should contain only a valid date in the format YYYYMMDD. If it does not, an error does not occur; instead, an invalid value is assigned to the receiving field. The results of using this value are undefined.
c
t
The sending field should only have a valid time in the format HHMMSS. If it does not, an error does not occur; instead, an invalid value is assigned to the receiving field. The results of using this value are undefined.
c
n
The sending field is scanned from left to right and only the digits 0-9 are transferred to the receiving field (right-justified) and padded on the left with zeros. All other characters are simply ignored.
c
x
Valid values for the sending field are 0-9 and capital letters A-F. The value is left-justified and padded on the right with zeros or truncated on the right. All characters after the first invalid value in the sending field are ignored.
p
c
The value is right-justified in the receiving field with the right-most byte reserved for a trailing sign. The sign is only displayed if the number is negative; therefore, positive numbers will be right-justified with a single trailing blank. In the event you try to move a positive value that contains as many digits as the receiving field is long, the system will use the entire length of the receiving field to contain the value without reserving the right-most byte for the sign. After considering the above, if the value in the sending field will not fit the receiving field, the number is truncated on the left. If truncation has occurred, the system indicates this by replacing the left-most digit with an asterisk (*). If the value does fit in the receiving field, leading zeros are suppressed. If the sending field is equal to zero, the receiving field receives a single zero.
p
d
The number is interpreted as the number of days since 0001/01/01, converted to a date, and stored internally in YYYYMMDD format.
p
t
The number is interpreted as the number of seconds since midnight, converted to 24-hour clock time, and stored internally in HHMMSS format.
d
p
The date is converted to a number representing the number of days since 0001/01/01.
t
p
The time is converted to a number representing the number of seconds since midnight.



Example below contains a demonstration program that performs sample data conversions.
EXAMPLE:
 1 report ztx0906.
 2 constants >(3) value '==>'.      "defines a constant named '>'
 3 data: fc(10)  type c value '-A1B2C3.4',
 4       fn(10)  type n,
 5       fp      type p,
 6       fd      type d,
7       ft      type t,
 8       fx(4)   type x,
 9       fc1(5)  type c value '-1234',
10       fc2(5)  type c value '1234-',
 11       fp1     type p value 123456789,
12       fp2     type p value '123456789-',
13       fp3     type p value 1234567899,
14       fp4     type p value 12345678901,
15       fp5     type p value 12345,
16       fp6     type p value 0.
17
18 fn = fc.       write: / fc, >, fn, 'non-numeric chars are ignored'.
19 fd = 'ABCDE'.  write: / fd, 'date and time fields are invalid'.
20 ft = 'ABCDE'.  write: / ft, '  when you load them with junk'.
21 fp = sy-datum. write: / sy-datum, >, fp, 'd->p: days since 0001/01/01'.
22 fp = sy-uzeit. write: / sy-uzeit, >, fp, 'd->t: secs since midnight'.
23 fx = 'A4 B4'.  write: / 'A4 B4', >, fx, 'ignore all after invalid char'.
24 fp = fc1.      write: / fc1, >, fp, 'allows leading sign'.
25 fp = fc2.      write: / fc2, >, fp, 'also allows trailing sign'.
26 fc = fp1.      write: / fp1, >, fc, 'rightmost byte reserved for sign'.
27 fc = fp2.      write: / fp2, >, fc, 'only negative numbers use it, but'.
28 fc = fp3.      write: / fp3, >, fc, '+ve nums that need it use it too'.
29 fc = fp4.      write: / fp4, >, fc, 'overflow indicated by leading *'.
30 fc = fp5.      write: / fp5, >, fc, 'leading zeros are suppressed'.
31 fc = fp6.      write: / fp6, >, fc, 'zero in = zero out'.
32 fp = ' '.      write: / ' ', >, fp, 'blanks in = zero out'.
OUTPUT:
-A1B2C3.4  ==> 0000001234 non-numeric chars are ignored
E   ABCD date and time fields are invalid
ABCDE0   when you load them with junk
1998/02/22 ==>         729,443  d->p: days since 0001/01/01
14:57:05 ==>          53,825  d->t: secs since midnight
A4 B4 ==> A4000000 ignore all after invalid char
-1234 ==>           1,234- allows leading sign
1234- ==>           1,234- also allows trailing sign
    123,456,789  ==> 123456789  rightmost byte reserved for sign
    123,456,789- ==> 123456789- only negative numbers use it, but
  1,234,567,899  ==> 1234567899 +ve nums that need it use it too
 12,345,678,901  ==> *345678901 overflow indicated by leading *
         12,345  ==>     12345  leading zeros are suppressed
              0  ==>         0  zero in = zero out
  ==>               0  blanks in = zero out

Wednesday 24 July 2013

JULY 24


Created various programs to illustrate the concept of CLEAR statements.

EXAMPLE-PROGRAMS for CLEAR Statements

Example-1:
Variables Set to Blanks or Zeros by the CLEAR Statement
 1 report ztx0904.
 2 tables ztxlfa1.
 3 data: f1(2) type c value 'AB',
 4       f2    type i value 12345,
 5       f3    type p value 12345,
 6       f4    type f value '1E1',
 7       f5(3) type n value '789',
 8       f6    type d value '19980101',
 9       f7    type t value '1201',
10       f8    type x value 'AA',
11       begin of s1,
12           f1(3) type c value 'XYZ',
13           f2    type i value 123456,
14           end of s1.
15 ztxlfa1-lifnr = 'XXX'.
16 ztxlfa1-land1 = 'CA'.
17 write: / 'f1=''' no-gap, f1 no-gap, '''',
18        / 'f2=''' no-gap, f2 no-gap, '''',
19        / 'f3=''' no-gap, f3 no-gap, '''',
20        / 'f4=''' no-gap, f4 no-gap, '''',
21        / 'f5=''' no-gap, f5 no-gap, '''',
22        / 'f6=''' no-gap, f6 no-gap, '''',
23        / 'f7=''' no-gap, f7 no-gap, '''',
24        / 'f8=''' no-gap, f8 no-gap, '''',
25        / 's1-f1=''' no-gap, s1-f1 no-gap, '''',
26        / 's1-f2=''' no-gap, s1-f2 no-gap, '''',
27        / 'ztxlfa1-lifnr=''' no-gap, ztxlfa1-lifnr no-gap, '''',
28        / 'ztxlfa1-land1=''' no-gap, ztxlfa1-land1 no-gap, ''''.
29 clear: f1, f2, f3, f4, f5, f6, f7, f8, s1, ztxlfa1.
30 write: / 'f1=''' no-gap, f1 no-gap, '''',
31        / 'f2=''' no-gap, f2 no-gap, '''',
32        / 'f3=''' no-gap, f3 no-gap, '''',
33        / 'f4=''' no-gap, f4 no-gap, '''',
34        / 'f5=''' no-gap, f5 no-gap, '''',
35        / 'f6=''' no-gap, f6 no-gap, '''',
36        / 'f7=''' no-gap, f7 no-gap, '''',
37        / 'f8=''' no-gap, f8 no-gap, '''',
38        / 's1-f1=''' no-gap, s1-f1 no-gap, '''',
39        / 's1-f2=''' no-gap, s1-f2 no-gap, '''',
40        / 'ztxlfa1-lifnr=''' no-gap, ztxlfa1-lifnr no-gap, '''',
41        / 'ztxlfa1-land1=''' no-gap, ztxlfa1-land1 no-gap, ''''.
OUTPUT:
f1='AB'
f2='    12,345 '
f3='         12,345 '
f4=' 1.000000000000000E+01'
f5='789'
f6='19980101'
f7='120100'
f8='AA'
s1-f1='XYZ'
s1-f2='   123,456 '
ztxlfa1-lifnr='XXX       '
ztxlfa1-land1='CA '
f1='  '
f2='         0 '
f3='              0 '
f4=' 0.000000000000000E+00'
f5='000'
f6='00000000'
f7='000000'
f8='00'
s1-f1='   '
s1-f2='         0 '
ztxlfa1-lifnr='          '
ztxlfa1-land1='   '

Description:
  • Line 2 defines field string ztxlfa1.
  • Lines 4 through 10 define variables of every type and give them default values.
  • Another field string, s1, is defined on line 11, with default values for each component.
  • Values are assigned to two of the components for field string ztxlfa1 on lines 15 and 16.
  • All variables and components of field strings are set to zeros and blanks by the clear statement on line 29. It can also be said that all variables and components are set to default initial values.
  • The write statements beginning on line 30 write out the cleared variables and components surrounded by single quotes and without intervening spaces between the quotes and the values they surround.
Example-2:
 Variables Filled with Characters Other than Blanks or Zeros Using the WITH Addition of the CLEAR Statement
 1 report ztx0905.
 2 tables ztxlfa1.
 3 data: f1(2) type c value 'AB',
 4       f2(2) type c,
 5       f3    type i value 12345,
 6       begin of s1,
 7           f1(3) type c value 'XYZ',
 8           f2    type i value 123456,
 9           end of s1.
10 write: / 'f1=''' no-gap, f1 no-gap, '''',
11        / 'f2=''' no-gap, f2 no-gap, '''',
12        / 'f3=''' no-gap, f3 no-gap, '''',
13        / 's1-f1=''' no-gap, s1-f1 no-gap, '''',
14        / 's1-f2=''' no-gap, s1-f2 no-gap, '''',
15        / 'ztxlfa1-lifnr=''' no-gap, ztxlfa1-lifnr no-gap, '''',
16        / 'ztxlfa1-land1=''' no-gap, ztxlfa1-land1 no-gap, '''',
17        /.
18 clear: f1 with 'X',
19        f2 with f1,
20        f3 with 3,
21        s1 with 'X',
22        ztxlfa1 with 0.
23 write: / 'f1=''' no-gap, f1 no-gap, '''',
24        / 'f2=''' no-gap, f2 no-gap, '''',
25        / 'f3=''' no-gap, f3 no-gap, '''',
26        / 's1-f1=''' no-gap, s1-f1 no-gap, '''',
27        / 's1-f2=''' no-gap, s1-f2 no-gap, '''',
28        / 'ztxlfa1-lifnr=''' no-gap, ztxlfa1-lifnr no-gap, '''',
29        / 'ztxlfa1-land1=''' no-gap, ztxlfa1-land1 no-gap, ''''. 
 Output:
f1='AB'
f2='  '
f3='    12,345 '
s1-f1='XYZ'
s1-f2='   123,456 '
ztxlfa1-lifnr='          '
ztxlfa1-land1='   '

f1='XX'
f2='XX'
f3='50,529,027 '
s1-f1='XXX'
s1-f2='1482184792 '
ztxlfa1-lifnr='##########'
ztxlfa1-land1='###'
Description:
  • Line 18 fills f1 with the letter X.
  • Line 19 fills f2 with the first byte of f1, also an X.
  • Line 20 fills f3 with the first byte of the literal 3. A numeric literal up to nine digits long is stored as a four-byte integer . f3 is filled with the first byte of this four-byte integer, essentially assigning garbage to f3.
  • Line 21 treats s1 as a type c variable and fills it with X. Component f1 is type c, so it receives valid values. Component f2 is type i and so receives invalid values.
  • Field string ztxlfa1 is filled with the first byte from the four-byte integer value 0, filling it with garbage. Garbage, in this case, is displayed as hash marks (#).

Tuesday 23 July 2013

JULY 23


Studied about system variables with example program , assignment statements and its types.

Working with System Variables:

There are 176 system variables available within every ABAP/4 program. we do not have to define them; they are automatically defined and are always available.
To display a list of system variables, we can display the DDIC structure syst. we can also display it by using the Dictionary: Initial Screen (se11), or by double-clicking on the name of any system variable in our program.

List of Commonly Used System Variables

Name
Description
sy-datum
Current date
sy-uzeit
Current time
sy-uname
Current user id
sy-subrc
Last return code
sy-mandt
Logon client
sy-pagno
Current output page number
sy-colno
Current output column number
sy-linno
Current output list line number
sy-vline
Vertical line
sy-uline
Horizontal line
sy-repid
Current report name
sy-cprog
Main program name
sy-tcode
Current transaction code
sy-dbcnt
Within a select, contains the current iteration counter. After the endselect, contains number of rows that match the where clause.
Example program displaying use of system variables.
Example-1:

Using Basic System Variables
 1 report ztx0901.
 2 tables ztxlfa1.
 3 parameters `land1 like ztxlfa1-land1 obligatory default 'US'.
 4 write: / 'Current date:', sy-datum,
 5        / 'Current time:', sy-uzeit,
 6        / 'Current user:', sy-uname,
 7        / 'Vendors having country code', `land1,
 8        /.
 9 select * from ztxlfa1
10     where land1 = `land1
11     order by lifnr.
12     write: / sy-dbcnt, ztxlfa1-lifnr.
13     endselect.
14 write: / sy-dbcnt, 'records found'.
15 if sy-subrc <> 0.
16     write: / 'No vendors exist for country', 'land1.
17     endif.

Output:
Current date: 1998/02/22
Current time: 14:38:24
Current user: KENGREENWOOD
Vendors having country code US

         1  1040
         2  1080
         3  1090
         4  2000
         5  V1
         6  V2
         7  V3
         8  V4
         9  V5
        10  V7 
        10 records found

Description:
  • Line 2 defines a field string ztxlfa1 exactly like the table of the same name.
  • Line 3 defines a single input parameter `land1.
  • On lines 4 through 6, the current date, time, and user id are written out from system variables sy-datumsy-uzeit, and sy-uname.
  • Line 7 writes out the country code that was entered in the parameter input field `land1 on the selection screen.
  • Line 8 writes out a blank line.
  • Line 9 selects records from table ztxlfa1 and places them one at a time into field string ztxlfa1.
  • Line 10 restricts the selection of records to only those having a country code equal to the one entered on the selection screen.
  • Line 11 causes the records to be sorted in ascending order by lifnr (vendor number).
  • Line 12 writes out the current iteration number from system variable
    sy-dbcnt and a vendor number from each record.
  • Line 13 marks the end of the select/endselect loop.
  • Line 14 writes out the total number of iterations of the select loop. This is the same as the number of records that matched the where clause using system variable sy-dbcnt.
  • On line 15, the return code from the select, contained in system variable sy-subrc, is tested. If it is zero, records were found. If it is non-zero, no records were found and a message is written out on line 16.

Assignment Statements:

An assignment statement assigns a value to a variable or field string. Three assignment statements are commonly used:
  • clear
  • move
  • move-corresponding

Using the clear Statement

The clear statement sets the value of a variable or a field string to zeros. If the data type is c, the value is instead set to blanks. Blanks and zeros are known as default initial values. It is often said that clear assigns default initial values to variables.

Syntax for the clear Statement

The following is the syntax for the clear statement.
clear v1 [with v2 | with 'A' | with NULL]
where:
  • v1 and v2 are variable or field string names.
  • 'A' is a literal of any length.

Using the move Statement

To move a value from one field to another, use the move statement. The entire contents or a portion thereof can be moved. Instead of move, we can use the assignment operator =, as shown below. They are both referred to as a move statement.

Syntax for the move Statement

The following is the syntax for the move statement. Operators and operands must be separated by spaces. Multiple assignment occurs from right to left.

move v1 to v2.
OR
v2 = v1.
OR
v2 = v1 = vm = vn.....
OR
move v1[+N(L)] to v2[+N(L)].
OR
v2[+N(L)] = v1[+N(L)].

where:
  • v1 is the sending variable or field string.
  • v2 is the receiving variable or field string.
  • N is an offset from the beginning of the variable or field string.
  • L is the number of bytes to move.

Right and Wrong Coding of Assignment

Right
Wrong
f1 = f2.f1=f2.
f1 = f2 = f3.f1=f2=f3.

Using the move-corresponding Statement

To perform a move from one field string to another where the data types and/or lengths do not match, we use the move-corresponding statement. It generates individual move statements for components with matching names. Components in the receiving field string that do not have a matching name in the sending field string are not changed. .

Syntax for the move-corresponding Statement

The following is the syntax for the move statement. Operators and operands must be separated by spaces. Multiple assignment occurs from right to left.
move-corresponding v1 to v2.
where:
  • v1 is the sending variable or field string.
  • v2 is the receiving variable or field string.

Monday 22 July 2013

JULY 22



Introduction to various types of statement in ABAP/4 with example.

ABAP Statements



Statements and Keywords

Types of statements in ABAP/4:
  • Declarative Statements
  • Modularization Statements
  • Control Statements
  • Call Statements
  • Operational Statements
  • Database Statements

The source code of an ABAP program consists of comments and ABAP statements.

Comments are distinguished by the preceding signs * (at the beginning of a line) and “ (at any

position in a line).

ABAP statements always begin with an ABAP keyword and are always concluded with a period

(.) . Statements can be several lines long; conversely, a line may contain more than one

statement.

ABAP statements use ABAP data types and objects.


The first element of an ABAP statement is the ABAP keyword. This determines the category of

the statements. 

The different statement categories are as follows:
  • Declarative Statements
These statements define data types or declare data objects which are used by the other

statements in a program or routine. The collected declarative statements in a program or routine

make up its declaration part.

Examples of declarative keywords:   TYPES, DATA, TABLES

  • Modularization Statements
These statements define the processing blocks in an ABAP program.

The modularization keywords can be further divided into:

1.     Event Keywords

We use statements containing these keywords to define event blocks. There are no

special statements to conclude processing blocks - they end when the next processing

block is introduced.

Examples of event keywords are: AT SELECTION SCREEN, START-OF-SELECTION, AT USER-COMMAND
    
    2. Defining keywords

we use statements containing these keywords to define subroutines, function modules,

dialog modules and methods. We conclude these processing blocks using the ENDstatements.

Examples of definitive keywords: FORM ..... ENDFORM, FUNCTION ... ENDFUNCTION,
MODULE ... ENDMODULE.


  • Control Statements
we use these statements to control the flow of an ABAP program within a processing block

according to certain conditions.

Examples of control keywords: IF, WHILE, CASE

  • Call Statements
we use these statements to call processing blocks that we have already defined using

modularization statements. The blocks we call can either be in the same ABAP program or in a

different program.

Examples of call keywords: PERFORM, CALL, SET USER-COMMAND, SUBMIT, LEAVE TO

  • Operational Statements

These keywords process the data that we have defined using declarative statements.

Examples of operational keywords: WRITE, MOVE, ADD

  • Database Statements
These statements use the database interface to access the tables in the central database system. 
There are two kinds of database statement in ABAP: Open SQL and Native SQL.

1.     Open SQL

Open SQL is a subset of the standard SQL92 language. It contains only Data Manipulation

Language (DML) statements, such as SELECT, INSERT, and DELETE. It does not contain any

Data Definition Language (DDL) statements (such as CREATE TABLE or CREATE INDEX).

Open SQL contains all of the DML functions from SQL92 that are common to all of the database systems supported by SAP. It also contains a few SAP-specific functions. ABAP programs that use only Open SQL statements to access the database are fully portable. The database interface converts the OPEN SQL commands into commands of the relevant database.

       2.   Native SQL

Native SQL statements are passed directly from the database interface to the database without

first being converted. It allows us to take advantage of all of our database’s characteristics in

your programs. In particular, it allows us to use DDL operations. The ABAP Dictionary uses

Native SQL for tasks such as creating database tables. In ABAP programs that use Native SQL statements are database-specific, because there is no standardized programming interface for SQL92.