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

Problem in IF condition

Former Member
0 Likes
2,314

Dear All,

Please see the below code.


tables  : LFM1.
data :  w_menge(16) type c.

LFM1-LFABC = 'X'.
W_MENGE = '0.499'.

IF LFM1-LFABC NE SPACE AND W_MENGE NE 0.

write :'SUCCESS'.

else.

write : 'FAIL'.
endif.

In the above code if the value of W_MENGE >= 0.5, the If condition gets satisfied and SUCCESS is displayed on screen.

But if the value of W_MENGE < 0.5, the codtion fails, and FAIL is displayed on screen.

Can any one please tell me the reason?

Regards

Arindam

11 REPLIES 11
Read only

Former Member
0 Likes
1,444

Dear,

You are using Data Type C for Your Qualityt field.

How could it do comparisons?

Regds,

Anil

Read only

Former Member
0 Likes
1,444

U use the below code.---

if it_table-menge GT 0.5.

write 'SUCCESS'.

else.

write 'FAIL'.

endif.

Read only

former_member434229
Active Participant
0 Likes
1,444

Hi,

Check whther you have selected 'Fixed Point Arithmetic' while creating the program.

To check it Open the program - > Goto -> Attributes. Check it is not selected.

Regards,

Ni3

Read only

Former Member
0 Likes
1,444

Hi,

Declare W_MENGE as follows.

DATA: W_MENGE type P DECIMALS 3. " NOTE dont use FLOAT because the value will be changed

This will work

Regards,

Smart Varghese

Edited by: smartvarghese on Jul 6, 2009 2:42 PM

Read only

Former Member
0 Likes
1,444

Hi,

define w_menge as float ie f.

LFABC = 'x'.

W_MENGE = '0.499'.

IF LFABC NE SPACE AND W_MENGE NE 0.

write :'SUCCESS'.

else.

write : 'FAIL'.

endif.

thanks.

Read only

Former Member
0 Likes
1,444

HI,

" Have 0 in quotes then you get the success message..
IF LFM1-LFABC NE SPACE AND W_MENGE NE '0'.

But still it is wrong..instead taking the char take the float type and compare the field. This is the right way.

Read only

0 Likes
1,444

Thanks Avinash. I did the same put quotes.

But it looks strange why it is working fine with value 0.5 or any value greater than that.

Read only

Former Member
0 Likes
1,444

Hi,

You try using 'NOT INITIAL' instead of NE 0 which is shown below

tables  : LFM1.
data :  w_menge(16) type c.
 
LFM1-LFABC = 'X'.
W_MENGE = '0.499'.
 
IF LFM1-LFABC NE SPACE AND W_MENGE IS NOT INITIAL."this works if the value is not blank
    " if you wish to check value non zero, then you can do as said above that is
    "IF LFM1-LFABC NE SPACE AND W_MENGE NE '0'.
 
write :'SUCCESS'.
 
else.
 
write : 'FAIL'.
endif.

Regards,

Manne.

Read only

0 Likes
1,444

I got the solution for this, BUt still dont understand reason behind it.

Why is it working fine with value 0.5 or any value greater than that?

Can anyone please let me know.

Regards

Arindam

Read only

0 Likes
1,444

That is because first you were trying to compare a character field with an integer value.

if v1 ne 0.

here 0 is taking as integer and for char variable v1 it is automatically converting to integer value for comparision.

so < 0.5 convert to integer is always 0.

and > 0.5 is convert to integer as 1.

so it is converting '0.499' to 0 and give you the wrong result.

But when you add qoutes ( '0') it became a comparision between two characters field so no conversion was made between character to int.

Now for that reason you are getting the correct result.

Regards

Shiba Prasad Dutta

Read only

Former Member
0 Likes
1,444

The answer is easy: rounding.

With values smaller than 0.5 it rounds to 0. Values greater than 0.5 round to 1.