Site icon DataFlair

SAP ABAP Operators with Examples

sap abap operators

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?

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.

OPERATOR SYMBOL DESCRIPTION EXAMPLE
Addition + Used to add two numerical values 25 + 3 = 28
Subtraction Used to subtract two numerical values 25 – 3 = 22
Multiplication * Used to multiply two numerical values 25 * 3 = 75
Division / Used to find quotient when one numerical value is divided by another numerical value 25 / 3 = 8
Modulus MOD Used to find remainder when one numerical value is divided by another numerical value 25 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.

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

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

OPERATOR SYMBOL DESCRIPTION EXAMPLE
Equality = To check whether two quantities are equal a = b would return false (as 2 is not equal to 4)
Inequality <> To check whether two quantities are unequal a <> 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 quantity a < 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 quantity a > 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 quantity a <= 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 quantity a >= b would return false (as 2 is less than 4)
Interval test a BETWEEN b AND c To 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 initial IS INITIAL To check if value of variable is the same as during assignment a IS INITIAL would be true
Is not initial IS NOT INITIAL To check if value of variable has been changed since assignment a 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) TYPE VARIABLE 2 (b) TYPE AUTOMATIC CONVERSION
I any b converted to type I
P any b converted to type P
D any b converted to type D
N C/X Both converted to type P
C X b 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.

OPERATOR MEANING
BIT-NOT It changes all ‘0’s to ‘1’s and vice versa
BIT-AND It uses AND operator on each bit, i.e. returns ‘1’ only if both bits are ‘1’, else returns ‘0’.
BIT-OR It uses OR operator on each bit, i.e. returns ‘0’ if both bits are ‘0’, else returns ‘1’
BIT-XOR It 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:

BIT1 NOT(BIT1) BIT 2 NOT (BIT 2) AND OR XOR
0 1 0 1 0 0 0
0 1 1 0 0 1 1
1 0 0 1 0 1 1
1 0 1 0 1 1 0

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) –

OPERATOR SYMBOL DESCRIPTION
Contains only A CO B Evaluates to true if A contains only characters from B
Does not contain only A CN B Evaluates to true if A contains at least one character not present in B
Contains any A CA B Evaluates to true if A contains at least one character present in B
Does not contain any A NA B Evaluates to true if A does not contain any character present in B
Contains string A CS B Evaluates to true if A contains the entire string B
Does not contain string A NS B Evaluates to true if A does not contain the entire string B
Contains pattern A CP B Evaluates to true if A contains the pattern in B
Does not contain pattern A NP B Evaluates 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.

Exit mobile version