SAP ABAP Operators with Examples

FREE Online Courses: Click for Success, Learn for Free - Start Now!

Welcome to the SAP ABAP Tutorial. In this tutorial, we are going to learn about SAP ABAP Operators – what they are, why they are used and how to use them. Let’s have you cosied up in using operators in ABAP.

What are Operators?

  • Operators are symbols used to perform various types of functions.
  • They manipulate variables and other values to produce a result.
  • Operators are combined with variables, constants, and literals to produce a meaningful expression that may produce a result.
  • Without operators, there is no meaning to an expression.

What are Operators in SAP ABAP?

ABAP provides a myriad of operators to be used to perform several activities. They can be categorized in the following types:

  1. Arithmetic Operators
  2. Logical/ Comparison Operators
  3. Bitwise Operators

Let us see each type of operator in detail.

1. Arithmetic Operators in ABAP

As the name goes, arithmetic operators are used to carrying out mathematical operations. They are used similarly to how we use them in algebra.

Let us see a few arithmetic operators.

OPERATORSYMBOLDESCRIPTIONEXAMPLE
Addition+Used to add two numerical values25 + 3 = 28
SubtractionUsed to subtract two numerical values25 – 3 = 22
Multiplication*Used to multiply two numerical values25 * 3 = 75
Division/Used to find quotient when one numerical value is divided by another numerical value25 / 3 = 8
ModulusMODUsed to find remainder when one numerical value is divided by another numerical value25 MOD 3 = 1

Let’s see examples on how to use Arithmetic Operators in ABAP –

REPORT ZR_SS_DATAFLAIR_SAMPLE_001.

DATA:
      A TYPE I VALUE 23,
      B TYPE I VALUE 5,
      Sum TYPE I,
      Difference TYPE I,
      Product TYPE I,
      Quotient TYPE I,
      Remainder TYPE I.

Sum =  A + B.
WRITE / Sum.

Difference =  A - B.
WRITE / Difference.

Product =  A * B.
WRITE / Product.

Quotient =  A / B.
WRITE / Quotient.

Remainder =  A MOD B.
WRITE / Remainder.

Output

28
18
115
5
3

2. Logical/Comparison Operators in ABAP

Logical operators are used for comparison between two values. They are usually used as tests between two variables, and return a true/false value based on the result.

Let us see a few logical operators.

Consider a = 2, b = 4, c = 0

OPERATORSYMBOLDESCRIPTIONEXAMPLE
Equality=To check whether two quantities are equala = b would return false (as 2 is not equal to 4)
Inequality<>To check whether two quantities are unequala <> b would return true (as 2 is not equal to 4)
Less than<To check whether the first quantity is lesser in value than the second quantitya < b would return true (as 2 is less than 4)
Greater than>To check whether the first quantity is greater in value than the second quantitya > b would return false (as 2 is less than 4)
Less than or equal to<=To check whether the first quantity is lesser or equal in value than the second quantitya <= b would return true (as 2 is less than 4)
Greater than or equal to>=To check whether the first quantity is greater or equal in value than the second quantitya >= b would return false (as 2 is less than 4)
Interval testa BETWEEN b AND cTo check whether quantity ‘a’ lies in value in between quantities ‘b’ and ‘c’a BETWEEN b AND c would return true (as 2 is between 0 & 4)
Is initialIS INITIALTo check if value of variable is the same as during assignmenta IS INITIAL would be true
Is not initialIS NOT INITIALTo check if value of variable has been changed since assignmenta IS NOT INITIAL would be false

if either one of length or data type does not match for the two variables being operated upon, ABAP will automatically convert either one or both variables. The rules of conversion are as follows – 

VARIABLE 1 (a) TYPEVARIABLE 2 (b) TYPEAUTOMATIC CONVERSION
Ianyb converted to type I
Panyb converted to type P
Danyb converted to type D
NC/XBoth converted to type P
CXb converted to type C

Let’s see examples on how to use Logical Operators in ABAP –

REPORT ZR_SS_DATAFLAIR_SAMPLE_001.
DATA:
      A TYPE I VALUE 2,
      B TYPE I VALUE 4,
      C TYPE I VALUE 0.


IF A = B.
  WRITE: / 'A is equal to B'.
ENDIF.

IF A <> B.
  WRITE: / 'A is not equal to B'.
ENDIF.

IF A < B.
  WRITE: / 'A is lesser than B'.
ENDIF.

IF A > B.
  WRITE: / 'A is greater than B'.
ENDIF.

IF A <= B.
  WRITE: / 'A is lesser than or equal to B'.
ENDIF.

IF A >= B.
  WRITE: / 'A is greater than or equal to B'.
ENDIF.

IF A BETWEEN C AND B.
  WRITE: / 'A lies between B and C'.
ENDIF.

Output

A is not equal to B
A is lesser than B
A is lesser than or equal to B
A lies between B and C

3. Bitwise Operators in ABAP

We also have bitwise operators in ABAP for boolean-level operations.

OPERATORMEANING
BIT-NOTIt changes all ‘0’s to ‘1’s and vice versa
BIT-ANDIt uses AND operator on each bit, i.e. returns ‘1’ only if both bits are ‘1’, else returns ‘0’.
BIT-ORIt uses OR operator on each bit, i.e. returns ‘0’ if both bits are ‘0’, else returns ‘1’
BIT-XORIt uses an XOR operator on each bit, i.e. returns ‘0’ if both bits are equal, and returns ‘1’ if they are unequal.

Here is an example of how the above operators work:

BIT1NOT(BIT1)BIT 2NOT (BIT 2)ANDORXOR
0101000
0110011
1001011
1010110

Let’s see examples of how to use Bitwise Operators in ABAP – 

REPORT ZR_SS_DATAFLAIR_SAMPLE_001.
DATA:
      A TYPE X VALUE '24',
      B TYPE X VALUE '12',
      RES_AND TYPE X,
      RES_OR TYPE X,
      RES_NOT TYPE X,
      RES_XOR TYPE X.

RES_AND = A BIT-AND B.
WRITE: / 'A BIT-AND B = ', RES_AND.

RES_OR = A BIT-OR B.
WRITE: / 'A BIT-OR B = ', RES_OR.

RES_XOR = A BIT-XOR B.
WRITE: / 'A BIT-XOR B = ', RES_XOR.

RES_NOT = BIT-NOT A.
WRITE: / 'BIT-NOT A = ', RES_NOT.

Output

A BIT-AND B = 00
A BIT-OR B = 36
A BIT-XOR B = 36
BIT-NOT A = DB

4. Character String Operators in ABAP

As the name suggests, Character String Operators in ABAP compare characters (single character text) with strings (multiple character text) and evaluate results according to its presence or absence in the string.

To simplify, let’s see types and examples:

For the description, assume we have two strings: A & B, we perform operations on them as follows (operator is in bold) –

OPERATORSYMBOLDESCRIPTION
Contains onlyA CO BEvaluates to true if A contains only characters from B
Does not contain onlyA CN BEvaluates to true if A contains at least one character not present in B
Contains anyA CA BEvaluates to true if A contains at least one character present in B
Does not contain anyA NA BEvaluates to true if A does not contain any character present in B
Contains stringA CS BEvaluates to true if A contains the entire string B
Does not contain stringA NS BEvaluates to true if A does not contain the entire string B
Contains patternA CP BEvaluates to true if A contains the pattern in B
Does not contain patternA NP BEvaluates to true if A does not contain the pattern in B

Let’s see examples on how to use Character String Operators in ABAP –

REPORT ZR_SS_DATAFLAIR_SAMPLE_001.
DATA:
      P(10) TYPE C VALUE 'APPLE',
      Q(10) TYPE C VALUE 'PINEAPPLE'.

IF P CA Q.
  WRITE: / 'P contains at least one character of Q'.
ENDIF.

IF P NA Q.
  WRITE: / 'P does not contain any character of Q'.
ENDIF.

IF P CO Q.
  WRITE: / 'P is solely composed of the characters in Q'.
ENDIF.

IF P CN Q.
  WRITE: / 'P contains characters that are not in Q'.
ENDIF.

IF P CS Q.
  WRITE: / 'P contains the character string Q'.
ENDIF.

IF P NS Q.
  WRITE: / 'P does not contain the character string Q'.
ENDIF.

IF P CP Q.
  WRITE: / 'P contains the pattern in Q'.
ENDIF.

IF P NP Q.
  WRITE: / 'P does not contain the pattern in Q'.
ENDIF.

Output

P contains at least one character of Q
P is solely composed of the character sin Q
P does not contain the character string Q
P does not contain the pattern in Q

Summary

Today, we learnt about the various operators available in SAP ABAP. We learnt the meaning of operators, why and how they are used. We also learnt through examples – both with code and without, on how to use these operators.

Thus, these are the various types of operators present in ABAP. They are used to manipulate, compare and operate on variables and constants.

We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *