Tutorial Decisions in SAP ABAP

Tutorial Decisions in SAP ABAP
The Decision making structure has one or more conditions to be evaluated or tested by the program, accompanied by statements or statements to be executed, if conditions are determined to be correct, and optionally, other statements to be executed,


Following are the general forms of decision structure used in most programming languages:
decision_making_sap_abap

The ABAP programming language provides the following types of decision-making statements:

NoStatement & Description
1IF Statement
IF statements consist of logical expressions followed by one or several statements.
2IF.. Else Statement
IF statements can be followed by optional ELSE statements that execute when the expression is false.
3Nested IF Statement
You can use one IF or ELSEIF statement in another IF or ELSEIF statement..
4CASE Control Statement
CASE is used when we need to compare two or more fields or variables.
Baca Juga: Tutorial Dasar SAP ABAP

IF Statement

Syntax
 
IF<condition_1>.
  
<Statements...>.
ENDIF
if_statement_sap_abap
Contoh:
?
1
2
3
4
5
6
Report YH_SEP_15. 
Data Title_1(20) TYPE C.
Title_1 = 'Tutorials'
IF Title_1 = 'Tutorials'
write 'This is IF statement'
ENDIF.
Outpunya adalah
This is IF statement

IF.. Else Statement

Syntax
IF<condition_1>.  
   <statement block 1>.  
ELSE.   
   <statement block 2>.  
ENDIF.
if_else_statement_sap_abap
Contoh:
?
1
2
3
4
5
6
7
8
9
10
11
Report YH_SEP_15.
   
Data Title_1(20) TYPE C. 
     Title_1 = 'Tutorials'.
  
IF Title_1 = 'Tutorial'
   write 'This is IF Statement'
ELSE. 
   write 'This is ELSE Statement'.
   
ENDIF.
Outpunya adalah
This is ELSE Statement.

Nested IF Statement

Syntax
IF<condition_1>. 
<statement block>.  
IF<condition_2>. 
<statement block>.  
ELSE. 
<statement block>.  
ENDIF. 
ELSE <statement block>.  
ENDIF.
Example:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Report YH_SEP_15.
  
Data: Title_1(10) TYPE C,
      Title_2(15) TYPE C,
      Title_3(10) TYPE C.
    
Title_1 = 'ABAP'.
Title_2 = 'Programming'.
Title_3 = 'Tutorial'.
   
IF Title_1 = 'ABAP'.
   
IF Title_2 = 'Programming'
   IF Title_3 = 'Tutorial'
      Write 'Yes, It’s Correct'.
ELSE. 
Write 'Sorry, It’s Wrong'.
  
ENDIF.
     
ENDIF. 
ENDIF.
Outpunya adalah
Yes, It’s Correct.

CASE Control Statement

Syntax
CASE <field>. 
 
WHEN <abc>. 
   <statement block>.
   
WHEN <def>. 
   <tatement block>.
   
WHEN <pqr>. 
   <statement block>. 
...... 
...... 
......  
WHEN <xyz>. 
   <statement block>. 
  
WHEN OTHERS. 
   <statement block>.  
ENDCASE.
Example:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
eport YH_SEP_15.
   
Data: Title_1(10) TYPE C, 
   Title_2(15) TYPE C. 
  
Title_1 = 'ABAP'.
Title_2 = 'Programming'
 
CASE Title_2.
   
WHEN 'ABAP'.
   Write 'This is not the title'
  
WHEN 'Tutorials'.
   Write 'This is not the title'
  
WHEN 'Limited'.
   Write 'This is not the title'.
  
WHEN 'Programming'.
   Write 'Yes, this is the title'.
  
WHEN OTHERS.
   Write 'Sorry, Mismatch'.
   
ENDCASE.
The output is
Yes, this is the title.
Blogger
Disqus

No comments