0% found this document useful (0 votes)
4 views16 pages

3. Conditional Statement

The document provides an overview of conditional statements in SAP ABAP, including IF...ENDIF, IF-ELSE...ENDIF, IF-ELSEIF-ELSE...ENDIF, and CASE-WHEN...ENDIF structures. It also covers looping constructs, control break statements, and string operations such as concatenation, split, condense, strlen, find, and translate. Each section includes syntax and examples to illustrate the concepts effectively.

Uploaded by

Laksh Mhr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views16 pages

3. Conditional Statement

The document provides an overview of conditional statements in SAP ABAP, including IF...ENDIF, IF-ELSE...ENDIF, IF-ELSEIF-ELSE...ENDIF, and CASE-WHEN...ENDIF structures. It also covers looping constructs, control break statements, and string operations such as concatenation, split, condense, strlen, find, and translate. Each section includes syntax and examples to illustrate the concepts effectively.

Uploaded by

Laksh Mhr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Conditional Statement

Types of conditional statements:


1. IF...ENDIF
If and ENDIF are control statements used for conditional execution.
The IF statement is used to perform a conditional test. It evaluates a condition and
execute a block of code if the condition is true. The basic syntax of the IF statement is:
Syntax: If<condition>.
<statement block>
Endif.

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:

data: Num1 type i.

num1 = 10.

IF num1 < 20.


write /'The numer is smaller'.

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:

data: Num1 type i.

num1 = 10.

IF num1 < 05.


write /'The numer is smaller'.
Else.
write:/ 'The number is less than'.

ENDIF.

3. IF-ELSEIF-ELSE...ENDIF

The IF-ELSEIF-ELSE...ENDIF statement in SAP ABAP allows you to handle multiple


conditions in a structured and readable manner. It's particularly useful when you have
several alternative conditions to check.

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.

DATA lv_input(2) TYPE n VALUE 5.

DO 5 times.
write: / lv_input.

ENDDO.

While Statement.

Syntax:

While.

<Statement block>.

Endwhile.
DATA num1 TYPE i.

num1 = 15.

WHILE num1 < 50.


write / num1.
num1 = num1 + 1.

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.

1. Create a program using exit statement.


DATA: num1 TYPE i.
num1 = 10.

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.

DATA: num1 TYPE i.


num1 = 10.
DO 10 TIMES.
num1 = num1 + 1.
write: / num1.
IF num1 = 15.
continue.
ENDIF.
ENDDO.

Output.
4. Create a program using check statement.

DATA: num1 TYPE i.

num1 = 10.

DO 10 TIMES.
num1 = num1 + 1.
check num1 = 15.
Write : / 'The result is =', num1.

ENDDO.
Output

String operation in SAP ABAP


String are set of characters arranged in sequence. String also called as character
string. Character string data type is used to declare the character string in SAP
ABAP.

String operation.
1. Concatenation
2. Split
3. Condense
4. Strlen
5. Find
6. Translate

1. Concatenation: It is used to join two string and form third string.

Example:

data: input1(10) type c VALUE 'Advance',


input2(10) type c value 'Business',
input3(10) type c value 'Application',
Input4(10) type c value 'Programing',
Result type string.

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:

DATA: input1 TYPE string VALUE 'Advance Business Application Progring',

Result1 TYPE string,


Result2 TYPE string,
Result3 TYPE string,
Result4 TYPE string.
WRITE:/ 'Before split:', input1.
SPLIT input1 AT space INTO result1 result2 result3 result4.
WRITE: / 'After split:'.
WRITE: / result1,
/ result2,
/ result3,
/ result4.

Output:

3. Condense: It is used to remove leading and trailing spaces and convert a


sequence of spaces into single space.
Example:
DATA: input1 TYPE string VALUE ' Advance Business Applicatio
Programing'.

WRITE: 'Before Condense:', input1.


CONDENSE input1.

*CONDENSE input1 no-gap.

WRITE:/ 'After condese:', input1.

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:

5. Find: The purpose of find is to find a particular pattern in a string.


Example:

DATA: input1 TYPE string VALUE 'Advance Business Applicatio Programing'.

find 'Advance' in input1.

IF sy-subrc = 0.
write : 'Successful', sy-subrc.
else.
write: 'unsuccessful', sy-subrc.

ENDIF.

6. Translate: The purpose of translate is to convert the string to upper case


or lower case.
Example:

DATA: input TYPE string VALUE 'Advance Business Applicatio Programing',


input1 TYPE string VALUE 'Advance Business Applicatio Programing'.
TRANSLATE input TO LOWER CASE.
WRITE:'Lower case:', input.
TRANSLATE input1 TO UPPER CASE.
WRITE: / 'Upper case:', input1.

Output:

You might also like