✒️ABAP El control del flujo de datos
ABAP El control del flujo de datos
Data Flow Control
Conditionals
A condition in ABAP triggers the execution of specific statements or sets of statements based on the evaluation of a logical expression. These expressions result in either TRUE or FALSE.
When a logical expression is TRUE, a designated set of statements is executed; otherwise, an alternative set of statements is carried out. In ABAP, conditions can be represented using the IF-ENDIF statement. For instance:
DATA: v_grade(1) TYPE c VALUE 'B'.
START-OF-SELECTION.
IF v_grade = 'A'.
WRITE:/ 'Excellent!'.
ELSE.
WRITE:/ 'Keep Improving.'.
ENDIF.
To demonstrate nested conditions within IF-ENDIF using the ELSEIF statement:
DATA: v_grade(1) TYPE c VALUE 'B'.
START-OF-SELECTION.
IF v_grade = 'A'.
WRITE:/ 'Excellent!'.
ELSEIF v_grade = 'B' OR 'C'.
WRITE:/ 'Good Job!'.
ELSE.
WRITE:/ 'Work Harder.'.
ENDIF.
In this example, the system executes the statements below ELSE if none of the conditions is valid.
An alternative method is using the CASE-ENDCASE statement:
DATA: v_grade(1) TYPE c VALUE 'B'.
START-OF-SELECTION.
CASE v_grade.
WHEN 'A'.
WRITE:/ 'Excellent!'.
WHEN 'B' OR 'C'.
WRITE:/ 'Good Job!'.
WHEN OTHERS.
WRITE:/ 'Work Harder.'.
ENDCASE.
If none of the conditions is valid, the system executes the statements below WHEN OTHERS.
Regardless of the approach—IF-ENDIF or CASE-ENDCASE—logical operators are essential in ABAP:
- AND
- OR
- Equal = EQ
- Not equal <> NE
- Greater than > GT
- Lower than < LT
- Greater than or equal >= GE
- Lower than or equal <= or LE
* Si el sy-subrc es igual a cero
IF sy-subrc EQ 0 OR sy-subrc = 0. "Ambas expresiones lógicas son identicas
WRITE:/'='.
* Si el sy-subrc es distinto de cero
ELSEIF sy-subrc NE 0 OR sy-subrc <> 0. "Ambas expresiones lógicas son identicas
WRITE:/'<>'.
* Si el sy-subrc es mayor a cero
ELSEIF sy-subrc GT 0 OR sy-subrc > 0. "Ambas expresiones lógicas son identicas
WRITE:/'>'.
* Si el sy-subrc es menor a cero
ELSEIF sy-subrc LT 0 OR sy-subrc < 0. "Ambas expresiones lógicas son identicas
WRITE:/'<'.
* Si el sy-subrc es mayor o igual a cero
ELSEIF sy-subrc GE 0 OR sy-subrc >= 0. "Ambas expresiones lógicas son identicas
WRITE:/'>='.
* Si el sy-subrc es menor o igual a cero
ELSEIF sy-subrc LE 0 OR sy-subrc <= 0. "Ambas expresiones lógicas son identicas
WRITE:/'<='.
ENDIF.
Loops
A loop in programming signifies a sequence of code instructions that iteratively execute until the assigned condition becomes false. In ABAP, different loop structures are available.
- Using LOOP-ENDLOOP designed for working with internal tables:
LOOP AT it_students INTO wa_student WHERE grade >= 'C'.
WRITE:/ wa_student-id,
wa_student-name,
wa_student-grade.
ENDLOOP.
- Another loop structure in ABAP is DO-ENDDO, suitable for working with variables:
DO 10 TIMES.
v_counter = v_counter + 1.
WRITE:/ v_counter.
ENDDO.
- Controlling DO Exits:
DO.
ADD 1 TO gv_numero.
IF gv_numero = 4.
WRITE: / 'Es la vuelta 4'.
CONTINUE.
ENDIF.
IF gv_numero > 8.
EXIT.
ENDIF.
gv_resultado = 1 * gv_numero.
WRITE: / '1 x ', gv_numero, ': ', gv_resultado.
ENDDO.
- While there is an option to use WHILE-ENDWHILE in ABAP, it's rarely used in practice.
DATA: gv_numero TYPE n LENGTH 2,
gv_resultado TYPE n LENGTH 2.
WHILE gv_numero < 10.
ADD 1 TO gv_numero.
gv_resultado = 2 * gv_numero.
WRITE: / '2 x ', gv_numero, ': ', gv_resultado.
ENDWHILE.
Accessing INDEX iterator:
When you are in a loop for an internal table (LOOP AT), you should use SY-TABIX.
If you are in another loop (DO or WHILE), you should use SY-INDEX.
Control Statements
Control statements are employed to conclude the processing of a loop, subroutine, or an entire program. ABAP provides several control statements:
- CHECK: Verifies if a condition is true, skipping subsequent statements within a loop if false, and continuing with the next iteration.
- EXIT: Exits a loop when executed within it, exits a subroutine when executed within, and terminates the program outside of a loop or subroutine.
- CONTINUE: Exclusively used within a loop. After execution, the system proceeds with the next iteration of the loop.
- STOP: Concludes the running program, executing the END-OF-SELECTION event if present.
- LEAVE: Terminates the running program without executing the END-OF-SELECTION event.
Careful consideration is essential while utilizing CHECK, EXIT, CONTINUE, LEAVE, and STOP as they directly influence the control flow in an ABAP program.
E.g.
REPORT ztest_abap_jega_4.
DATA: v_nro_vuelta TYPE i.
START-OF-SELECTION.
CLEAR v_nro_vuelta.
DO 10 TIMES.
ADD 1 TO v_nro_vuelta.
IF v_nro_vuelta = 6.
EXIT.
ENDIF.
ENDDO.
WRITE:/'El numero de vuelta final del ciclo fue: ', v_nro_vuelta.
"El numero de vuelta final del ciclo fue: 6
 
 
 
Sobre el autor
Publicación académica de Jaime Eduardo Gomez Arango, en su ámbito de estudios para la Carrera Consultor ABAP.
Jaime Eduardo Gomez Arango
Profesión: Ingeniero de Sistemas y Computación - España - Legajo: SW34C
✒️Autor de: 99 Publicaciones Académicas
🎓Cursando Actualmente: Consultor ABAP Nivel Avanzado
🎓Egresado del módulo:
Disponibilidad Laboral: FullTime
Presentación:
Ingeniero de sistemas y computación con 8 años de experiencia el desarrollo frontend & backend (react/node) y en cloud (aws), actualmente desarrollando habilidades en sap btp, ui5, abap y fiori.
Certificación Académica de Jaime Gomez