Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Control Structures "IF" containing arithmetic operator

Former Member
0 Likes
1,858
DATA ii TYPE i.
IF ( ii + 1 ) > 2.
  WRITE: / 'more than 2'.
ENDIF.

putting forward this error:

relational operator "+" is not supported

Is there any workaround or I have only left with inline functions and define macros.

DATA ii TYPE i.
IF ( ii + 1 ) > 2.
  WRITE: / 'more than 2'.
ENDIF.

putting forward this error:

relational operator "+" is not supported

Is there any workaround or I have only left with inline functions and define macros.

9 REPLIES 9
Read only

andreas_mann3
Active Contributor
0 Likes
1,393

Hi,

try:

DATA ii TYPE i.

add 1 to ii.

IF ( ii ) > 2.

WRITE: / 'more than 2'.

ENDIF.

Andreas

Read only

Former Member
0 Likes
1,393

HI Flora,

In abap you can't arithmatic ops in IF statement.

rather you do the ops and store the result in a variable, and use it in IF statement.

data sum type i.

......

......

sum = ii + 1.

if sum > 2

...

endif.

plz reward points if it helps you

Read only

Former Member
0 Likes
1,393

Hi,

In IF u can use only Logical Expressions.

Do a F1 and check for the synax.

IF logexp.

Used for case distinction.

Depending on whether the logical expression logexp is true or not, this statement triggers the execution of various sections of code enclosed by IF and ENDIF.

There are three different types:

IF logexp.

processing1

ENDIF.

If the logical expression is true, processing1 is executed. Otherwise, the program resumes immediately after ENDIF.

IF logexp.

processing1

ELSE.

processing2

ENDIF.

If the logical expression is true, processing1 is executed. Otherwise, processing2 is executed (see ELSE).

IF logexp1.

processing1

ELSEIF logexp2.

processing2

ELSEIF ...

...

ELSE.

processingN

ENDIF.

If the logexp1 is false, logexp2 is evaluated and so on. You can use any number of ELSEIF statements. If an ELSE statement exists, it always appears after all the ELSEIF statements.

Reward points if this helps.

Read only

jayanthi_jayaraman
Active Contributor
0 Likes
1,393

Hi,

The problem is because you used + in if st.

Try it in following way.

if i1 > 1.

write : / 'more than 2'.

endif.

Both are same logic.

Kindly reward points by clicking the star on the left of reply,if it helps.

Read only

former_member723628
Active Participant
0 Likes
1,393

Hi,

You can not use any arithmatic operators in a logical expression. If at it is required..a separate temporary variable should be assigned the value of arithmatic operation and then the temporary variable should be used in logical expression. Eg.

DATA ii TYPE i.

DATA var1 TYPE I.

var1 = ii + 1.

IF var1 > 2.

WRITE: / 'more than 2'.

ENDIF.

Also note that it is not recomended to use Macro's.

Cheers,

Gajendra Bhatt

Read only

Former Member
0 Likes
1,393

Hi Andreas Mann / Jayanthi Jayaraman : sorry, but that is not what i m looking for. the simple example was just to emphasise that i want to do arthmatic operation in IF statement.

Read only

0 Likes
1,393

Re-sending...

HI Flora,

In abap you can't do arithmatic ops in IF statement.

rather you do the ops and store the result in a variable, and use it in IF statement.

data sum type i.

......

......

sum = ii + 1.

if sum > 2

...

endif.

plz reward points if it helps you

Read only

0 Likes
1,393

Hi,

It is not possible to use arithmetic operation in if statement in ABAP as we do the same in other programming languages.

Even the following is not allowed.

if ( 1 + 1 ) ge 2.

write 'dsfd'.

endif.

So kindly reward points by clicking the star on the left of reply,if it helps.Kindly close the thread if your question is answered.

Read only

rainer_hbenthal
Active Contributor
0 Likes
1,393

As you read it is not allowed to have aritmetic expressions in an IF statement, but perhaps it is helpful that you can compare returning parameters from methods in an IF statement like this:

data: xyz type ref to z_own_class.

create object xyz.

IF xyz->method(parameters) = 5.

write:/ 'DO something'.

endif.

Message was edited by: Rainer Hübenthal