How to use if Statement in SAP
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 f1 contains 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.
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 n, both 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 x. x is converted to type c.
Conversions follow the same rules as those performed by the move statement. Type conversions are fully detailed in the ABAP/4 keyword documentation under the heading “Relational Operators for All Data Types.”
Popularity: 6% [?]






