Operators

Operators are symbols that used to describe the compiler to perform operations on variables and values.
ABAP supports various operators to process data by using variables or values.

Operator Types –

All ABAP operators are classified into below categories. Those are –

Operators that perform special operations for operands.

Operators Description Symbols
Declaration operators The declaration operators declare variables or field symbols for operands. DATA and FIELD-SYMBOL
Constructor operators The constructor operators construct values for data objects of specific data types in constructor expressions. NEW, VALUE, CONV, CORRESPONDING, CAST, REF, EXACT, REDUCE, FILTER, COND, and SWITCH

Operators that join multiple operands in a single expression.

Operators Description Symbols
Assignment operators The assignment operator = assigns the source field value to the target field.
For assignments, the casting operator ?= does a down cast.
The operator = also connects actual parameters with formal parameters.
?=, =
Arithmetic operators The arithmetic operators perform arithmetic operations on two or more numeric operands.
The characters + and – also behave as signs in arithmetic expressions.
+, -, *, /, DIV, MOD, and **
Bit operators The bit operators perform bit operations on two or more byte-like operands.
The bit operator BIT-NOT negates a byte-like operand.
BIT-AND, BIT-OR, BIT-NOT and BIT-XOR
String operators The string operator put together two character-like operands and forms a string expression. &&
Relational operators Relational operators used to compare two or more operands of any data type.

Relational operators and join two or more operands of any data type to form a relational expression or comparison expression.
There are additional relational operators for specific data types.
The predicate operator “IS” qualifying an operand.
=, , , =, IS, BETWEEN
Boolean operators Boolean operators used to join or combine two or more logical expressions. The result of a logical expression is either true or false.
The Boolean operator NOT negates the result of a logical expression.

The Boolean operators join the results of individual logical expressions with a logical expression.
AND, OR, NOT and EQUIV

Operator that joins two operands in compilations.

Operators Description Symbols
Literal operator The literal operator joins two literals or two strings with a literal or a string. &

Example –

Below example shows how the some of the operations coded in the program.

Code –

*&---------------------------------------------------------------------*
*& Report  Z_OPERATORS
*&---------------------------------------------------------------------*
*& Written by TutorialsCampus
*&---------------------------------------------------------------------*

REPORT  Z_OPERATORS.

* Declaring variables
DATA: W_NUM1 TYPE I VALUE 10,
      W_NUM2 TYPE I VALUE 3,
      W_ADD  TYPE I,
      W_OB1 TYPE X VALUE 'B2',
      W_NOT TYPE X,
      W_R TYPE STRING,
      W_OP1 TYPE I VALUE 40,
      W_OP2 TYPE I VALUE 60,
      W_OP3 TYPE I VALUE 80.

* Adding two variables (Arithmetic Operation)
W_ADD = W_NUM1 + W_NUM2.
WRITE: 'Result of W_NUM1 + W_NUM2: ', W_ADD.

* Applying BIT-NOT on W_OB1 and produces the result W_NOT.
* (Bitwise Operation)
W_NOT = BIT-NOT W_OB1.
WRITE: /'Result of BIT-NOT on W_OB1: ',W_NOT.

* Concatenation (&) of srings enclosed in two "|" (String Operation)
W_R = | HELLO | & | WORLD..!   |.
WRITE : /'Output-1:  ',W_R.

* Verifying W_OP1 less than W_OP2 (Relational Operation)
IF W_OP1 LT W_OP2.
  WRITE /'W_OP1 LESS THAN W_OP2'.
ENDIF.

* Logical AND expression (Boolean Operation)
IF ( W_OP1 LT W_OP2 ) AND ( W_OP2 LT W_OP3 ).
  WRITE /'Both logical expressions are true'.
ELSE.
  WRITE /'Both logical expressions are not true'.
ENDIF.

Output –

Operators Example Output

Explaining Example –

In the above example, each and every statement is preceeded with a comment to explain about the statement. Go through them to get clear understanding of example code.

W_ADD = W_NUM1 + W_NUM2, adds W_NUM1, W_NUM2 values and stores result in W_ADD.

W_NOT = BIT-NOT W_OP1, applies BIT-NOT on W_OP1 and produces the result W_NOT.

W_R = | HELLO | & | WORLD..! |, concatenation (&) of srings enclosed in two “|” and retains all spaces after concatenation.

IF W_OP1 LT W_OP2, verifying W_OP1 is LESS THAN (LT) W_OP2. If yes, display ‘W_OP1 LESS THAN W_OP2’.

IF ( W_OP1 LT W_OP2 ) AND ( W_OP2 LT W_OP3 ), verifies ( W_OP1 LT W_OP2 ) and ( W_OP2 LT W_OP3 ) are true or not. If true, displays ‘Both logical expressions are true’. otherwise, ‘Both logical expressions are not true’ gets displayed.

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *