onlyforbopi
3/7/2017 - 11:42 AM

ABAP - CONTROL FLOW AND OPERATORS

ABAP - CONTROL FLOW AND OPERATORS

*************************************************************************
* NESTED IF

DATA: 	query_run_time TYPE I VALUE 69,
		level TYPE I VALUE 2,
		query_type(11) TYPE C VALUE 'SAP HANA'.
		
IF query_run_time > 50.
		WRITE: / 'Runtime Exceeded Alert.'.
		IF level = 1.
			WRITE: / 'Fix Soon'.
		ELSEIF level = 2.
			WRITE: / 'Fix Now'.
		ELSEIF level = 3.
			WRITE: 'Alert Administrator'.
			IF query_type = 'SAP HANA'.
				WRITE: 'Alert George Whitman'.
			ELSEIF query_type = 'SAP BASIS'.
				WRITE: 'Alert Fred'.
			ELSEIF query_type = 'SAP Security'.
				WRITE: 'Alert Gary Burns.'.
			ENDIF.
		ENDIF.
ENDIF.
* IF / ELSE IF / ELSE
IF domain1 = 'university'.
	WRITE: / 'Hello Student'.
ELSEIF domain1 = 'company'.
	WRITE: / 'Hello Employee'.
ELSE.
	WRITE: / 'None'.
ENDIF.


DATA: score TYPE I VALUE 5.

IF score = 5.
	WRITE: 'Excellent'.
ELSEIF score = 4.
	WRITE: 'Good'.
ELSEIF score = 3.
	WRITE: 'Mediocre'.
ELSE.
	WRITE: 'Bad'.
ENDIF.
********************************************************
********************************************************
* CONTROL FLOW AND COMPARISON OPERATORS

* SIMPLE IF /ELSE
DATA: email1(25) TYPE C VALUE 'john_stewart@university.com',
		first_name1(10) TYPE C,
		last_name1(10) TYPE C,
		domain1(10) TYPE C.

* substring
domain1 = email1+13(10).
		
*IF condition.
*	code1.
*ELSE.
*	code2.
*ENDIF


IF domain1 = 'university'.
	WRITE: / 'Hello Student'.
ELSE.
	WRITE: / 'Hello Employee'.
ENDIF.

email1 = 'jasmine_chong@company.com'.
domain1 = email+14(7).

IF domain1 = 'university'.
	WRITE: / 'Hello Student'.
ELSE.
	WRITE: / 'Hello Employee'.
ENDIF.
*******************************************************************
* CASE STATEMENT
DATA: t_code(6) TYPE C VALUE 'SE80'.

CASE t_code.
	WHEN 'SE80'.
		WRITE: / , 'Obj navigator'.
	WHEN 'SE11'.
		WRITE: / 'Table Viewer'.
	WHEN 'SE38'.
		WRITE: / 'ABAP EDITOR'.
ENDCASE.
*Character String Operators
*Following is a list of character string operators −

* CO (Contains Only)
*Checks whether A is solely composed of the characters in B.

* CN (Not Contains ONLY)
* Checks whether A contains characters that are not in B.

* CA (Contains ANY)
* Checks whether A contains at least one character of B.

* NA (NOT Contains Any)
* Checks whether A does not contain any character of B.

* CS (Contains a String)
* Checks whether A contains the character string B.

* NS (NOT Contains a String)
* Checks whether A does not contain the character string B.

* CP (Contains a Pattern)
* It checks whether A contains the pattern in B.

* NP (NOT Contains a Pattern)
* It checks whether A does not contain the pattern in B.

* Example
REPORT YS_SEP_08. 
DATA: P(10) TYPE C VALUE 'APPLE',
      Q(10) TYPE C VALUE 'CHAIR'.
      IF P CA Q.
	
      WRITE: / 'P contains at least one character of Q'.
      ENDIF.
The above code produces the following output −

P contains at least one character of Q.
**************************************************************
* LOGICAL OPERATORS

DATA: 	sys_memory TYPE I VALUE 64,
		sys_storage TYPE I VALUE 1024,
		sys_version TYPE I VALUE 6,
		sys_hana TYPE I VALUE 0.
		
* AND NOT OR
* condition1 AND condition2
* condition1 OR condition2
* NOT condition1

IF ( sys_storage >= 256 ) AND ( sys_memory >= 8 ).
	WRITE: / 'DO the installation of SP01'.
ELSE.
	WRITE: / 'Dont do the installation of SP01'.
ENDIF.
	
IF ( sys_storage >= 256 ) OR ( sys_memory >= 8 ).
	WRITE: / 'DO the installation of SP01'.
ELSE.
	WRITE: / 'Dont do the installation of SP01'.
ENDIF.
	
IF ( sys_storage >= 256 ) AND NOT ( sys_memory >= 8 ).
	WRITE: / 'DO the installation of SP01'.
ELSE.
	WRITE: / 'Dont do the installation of SP01'.
ENDIF.

***************************************************************
*ABAP COMPARISON OPERATORS AND SAMPLE COMPARISONS

*= or EQ
*<> or NE
*<= or LE
*>= or GE
*> or GT
*< or LT
* IS INITIAL
* IS NOT INITIAL
* a1 BETWEEN a2 AND a3 (Interval test)


REPORT YS_SEP_08. 

DATA: A TYPE I VALUE 115,
      B TYPE I VALUE 119.
      IF A LT B.
      WRITE: / 'A is less than B'.
      ENDIF
      
      
REPORT YS_SEP_08. 

DATA: A TYPE I.
      IF A IS INITIAL.
      WRITE: / 'A is assigned'.
      ENDIF.
      
      
      
*Note − If the data type or length of the variables does not 
*match then automatic conversion is performed. Automatic type 
*adjustment is performed for either one or both of the values 
*while comparing two values of different data types. The conversion 
*type is decided by the data type and the preference order of 
*the data type.

*Following is the order of preference −
*If one field is of type I, then the other is converted to type I.
*If one field is of type P, then the other is converted to type P.
*If one field is of type D, then the other is converted to type D. But C and N types are not converted and they are compared directly. Similar is the case with type T.
*If one field is of type N and the other is of type C or X, both the fields are converted to type P.
*If one field is of type C and the other is of type X, the X type is converted to type C.

*ABAP also provides a series of bitwise logical operators that 
*can be used to build Boolean algebraic expressions. The bitwise 
*operators can be combined in complex expressions using parentheses 
*and so on.

*BIT-NOT
*Unary operator that flips all the bits in a hexadecimal
*number to the opposite value. For instance, applying this 
*operator to a hexadecimal number having the bit level 
*value 10101010 (e.g. 'AA') would give 01010101.

	
*BIT-AND
*This binary operator compares each field bit by bit using 
*the Boolean AND operator.

*BIT-XOR
*Binary operator that compares each field bit by bit using 
*the Boolean XOR (exclusive OR) operator.

*BIT-OR
*Binary operator that compares each field bit by bit using
*the Boolean OR operator.

*For example, following is the truth table that shows the values generated when applying the Boolean AND, OR, or XOR operators against the two bit values contained in field A and field B.

Field A	Field B	AND	    OR	  XOR
0	      0	      0	      0	    0
0	      1	      0	      1	    1
1	      0	      0	      1	    1
1	      1	      1	      1	    0
************************************************
* ARITHMETIC OPERATORS

* +
* -
* /
* *
* MOD

REPORT YS_SEP_08. 
DATA: A TYPE I VALUE 150, 
B TYPE I VALUE 50, 
Result TYPE I. 
Result =  A / B. 
WRITE / Result.
1. abaparithmetic.abap      : Arithmetic operators
2. abapcomparison.abap      : Comparison operators
3. abapstring.abap          : String operators
4. abapbitwise.abap         : Bitwise operators
5. abaplogical.abap         : Logical operators

6. ifelse.abap              : Control flow (if/else)
7. ifelseifelse.abap        : Control flow (if/elseifelse)
8. ifelsenested.abap        : Control flow (if/else/NESTED IF)
9. case.abap                : Control flow (Case Operator)