3. Conditional Statement
3. Conditional Statement
The ENDIF statement is used to mark the end of the block of code that is to be executed
if the condition specified in the IF statement is true.
Example:
num1 = 10.
ENDIF.
2. IF–ELSE...ENDIF
The IF-ELSE-ENDIF statement is used for conditional execution where you want to
execute one block of code if a condition is true and another block of code if the condition
is false.
Syntax: If<condition>.
<statement block>.
Else.
<statement block>.
Endif.
If condition is true, the code block immediately following the IF statement will be
executed.
If condition is false, the code block immediately following the ELSE statement will be
executed.
The ENDIF statement marks the end of the conditional block.
Example:
num1 = 10.
ENDIF.
3. IF-ELSEIF-ELSE...ENDIF
Syntax:If<condition>.
<statement Block>.
Elseif.
<statement block>.
Else.
<statement block>.
Endif.
The IF statement checks the first condition. If it is true, the code block immediately
following the IF statement is executed.
If the first condition is false, the program checks the next condition specified in ELSEIF (.
If condition2 is true, the corresponding code block executes.
This pattern continues with additional ELSEIF statements, each with its own condition to
check.
If none of the conditions specified in the IF, ELSEIF blocks are true, the code block
immediately following the ELSE statement is executed.
The ENDIF statement marks the end of the conditional block.
Example:
DATA: Day TYPE c.
day = 1.
IF Day = 1.
WRITE /'Monday'.
ELSEIF day = 2.
WRITE:/ 'Tuesday'.
ELSEIF day = 3.
WRITE / 'Wednesday'.
ELSEIF day = 4.
WRITE / 'Thursday'.
ENDIF.
4. CASE-WHEN...ENDIF
The case is evaluated, and depending on its value, the program will execute the block of
code associated with the matching WHEN clause.
If none of the values specified in the WHEN clauses matches the value of the case, the
block of code following WHEN OTHERS will be executed.
The ENDCASE statement marks the end of the CASE-WHEN block.
Syntax: Case<field>.
When<field1>.
<statement block>.
When<field2>.
<statement block>.
When others.
<statement block>.
Endcase.
Example:
DATA lv_input(2) TYPE n VALUE 5.
CASE lv_input.
WHEN 1.
WRITE: 'the output is', lv_input.
WHEN 2.
WRITE: 'the output is', lv_input.
WHEN 3.
WRITE: 'the output is', lv_input.
WHEN OTHERS.
WRITE: 'wrong input'.
ENDCASE.
5. LOOPING:
Conditional (Do-Enddo).
Un Conditional (While-Endwhile).
Do
Statement:
Syntax:
Do <N>
times. “
...
.
.
.
”
Enddo.
DO 5 times.
write: / lv_input.
ENDDO.
While Statement.
Syntax:
While.
<Statement block>.
Endwhile.
DATA num1 TYPE i.
num1 = 15.
ENDWHILE.
LOOP BREAK STATEMENTS
If you want to break the normal flow of an ABAP loop, you need control break
statements. ABAP provide three-loop control statements, i.e., EXIT, STOP and
CONTINUE.
➢ Exit statement: it will come out of the loop but it will stay inside the program.
➢ Stop Statement: it will come out of the loop and come out of the program.
➢ Continue statement: it will Skip the current iteration and go for the next iteration.
DO 10 TIMES.
WRITE:/ num1.
num1 = num1 + 1.
IF num1 = 15.
EXIT.
ENDIF.
ENDDO.
Output
2. Create a program using stop statement.
DATA: num1 TYPE i.
num1 = 10.
DO 10 TIMES.
WRITE:/ num1.
num1 = num1 + 1.
IF num1 = 15.
stop.
ENDIF.
ENDDO.
Output
3. Create a program using continue statement.
Output.
4. Create a program using check statement.
num1 = 10.
DO 10 TIMES.
num1 = num1 + 1.
check num1 = 15.
Write : / 'The result is =', num1.
ENDDO.
Output
String operation.
1. Concatenation
2. Split
3. Condense
4. Strlen
5. Find
6. Translate
Example:
concatenate input1 input2 input3 input4 into result SEPARATED BY ' '.
write: / result.
Output
2. Split: It is used to split the content of a field into two or more fields.
Example:
Output:
Output.
4. Strlen:
The STRLEN function is used to determine the length of a string. It returns the number of
characters in a given string. This function is particularly useful when you need to perform
operations or validations based on the length of a string.
Example:
DATA: input1 TYPE string VALUE ' Advance Business Applicatio Progr
aming'.
data: length1(2) type n.
WRITE: 'Before Condense:', input1.
length1 = strlen( input1 ).
write:/ length1.
CONDENSE input1.
WRITE:/ 'After condese:', input1.
length1 = strlen( input1 ).
write:/ length1.
Output:
IF sy-subrc = 0.
write : 'Successful', sy-subrc.
else.
write: 'unsuccessful', sy-subrc.
ENDIF.
Output: