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

IF statement Execution Error

anurag_singh16
Participant
0 Likes
8,654

Hi All,

Sorry to bother but I didn't know how to put up the question in better way than this.

If you have another thread for this query, kindly lead me to that thread.

I have a condition in code and the problem is that when the condition check is reached, the code is exited irrespective of the result of IF condition i.e., neither True nor False part is getting executed.

Please find below the code to understand the scenario.

1 is the IF condition.

2 is the True part of IF condition.

3 is the area of code which is out of so many nested conditions and is getting executed just after 1 instead of True or False part.

So, why is 2 not getting executed even if the value of 1 is True?

I am not able to figure out why is it happening and how to fix it.

Thanks,

Anurag

26 REPLIES 26
Read only

tamitdassharma
Active Participant
0 Likes
8,087

Hello Anurag,

Replace your first IF condition with a range table.

Something like IF lv_flstr IN lt_range_table.

This will solve your issue.

Also, if you want to go ahead with the code that you have written, replace the AND with an OR and it should work fine.

Read only

matt
Active Contributor
0 Likes
8,087

This is not good advice. You have no idea where the lv_low and lv_high are being set, nor the context of the program.

You certainly cannot declare "this will solve your issue".

Anyway, it's far more important for the OP to understand why there's an issue with his code, then he just gets a "fix". If he follows your advice and by some chance it works, then all he's doing is programming by coincidence.

Read only

0 Likes
8,087

Hello matthew.billingham,

It is clearly evident from the piece of code posted by anurag.singh16 that he is trying to execute a piece of code - labelled 2 in the screenshot when the condition in label 1 is fulfilled.

If you read the statement properly it is clear that he is comparing the value of lv_flstr to be in between lv_low and lv_high including both the limits. Hence I have suggested using a range table in place of that. I hope you will understand it better if you try to dry run the statement.

For the second suggestion of using OR. Since anurag has used the and operator and considering the value of lv_flstr to be greater than equal to lv_low the predecessor of the AND operator always forms to be true and the second part is considered to be false. And we all know any comparison including AND operator like (True AND False) will always result in false.

This is not programming by coincidence but understanding each line of code.

Hope you will try to understand answers provided by community users before criticizing.

Read only

8,087

dassharma : criticizing is always a good thing if it's fair and argumented (not done in a "lynch mob" way), and I find Matthew's comment fair and argumented, sorry to say that. Now, about your answer, I don't understand how it would help the OP solve his issue. Matthew's answer is better: it doesn't go in the "true" block of the IF because the condition is false, simple as that. Now, probably there's something tricky somewhere, and the question doesn't give clues what it can be... PS: please, don't bold your last sentences, it sounds like "yelling".

Read only

matt
Active Contributor
8,087

Sorry dassharma but I disagree.

I'm well aware of the fact that he's doing a simple "BETWEEN". And there's nothing wrong with that.

I find your suggestion that he convert that into a range is over complicating the issue. A range could contain anything - include patterns, exclusions etc. IN range is semantically very different from a simple comparison of low and high. Since programming should be a specific as possible to avoid unwanted side effects, it's not a good idea to add a generalisation such as a range, when a simple between test is what is required. If you've ever programmed in HR or used time-dependent infoobjects in BW, you'll frequently use BEGDA and ENDDA. To use a range to see if something lies between begda and endda would not be sensible.

On that basis I say your advice is not good.

Furthermore replacing the between with a range could end up with him programming by coincidence if he doesn't understand why the code is failing the first place.

On your second point concerning OR (which I've not commented on so far), unless I've entirely misunderstood, you're simply wrong. The OP gave us these values:

  • lv_flstr -> 85
  • lv_low -> 80
  • lv_high -> 100

DATA(lv_flstr) = 85.
DATA(lv_low) = 80.
DATA(lv_high) = 100.

IF lv_flstr GE lv_low AND lv_flstr LE lv_high.
  WRITE / 'In between'.
ELSE.
  WRITE / 'Not in between'.
ENDIF.
Is 85 greater than or equal to 80. True
Is 85 less than or equal to 100. True.

Output "In between".

You propose

If lv_flstr GE lv_low OR lv_flstr LE lv_high.

Consider lv_low = 80 and lv_high = 100. The following values of lv_flstr will give this clause as true.
75, 80, 105.
Which is probably not what he wants.

Read only

matt
Active Contributor
8,087

"So, why is 2 not getting executed even if the value of 1 is True?"

Because the value of 1 is not true.

Set a breakpoint at 1. Run your program. Examine the values of

  • lv_flstr
  • lv_low
  • lv_high

If you still can't figure out the issue from that, then comment on this answer with the values of these variables, and their defined types.

Read only

0 Likes
8,087

Hi Matthew,

The value of below variables at run time is as follows:

  • lv_flstr -> 85
  • lv_low -> 80
  • lv_high -> 100

So I guess, the value of IF is True. right ?

And also if it's False, then it should reach out to Else part of the same IF condition.

Now suggest.

Read only

8,087

He also asked for their defined types, since that might also have an influence.

Read only

matt
Active Contributor
8,087

anurag.singh16 The value of the IF is true. But only if you've got your typing right.

"If you still can't figure out the issue from that, then comment on this answer with the values of these variables, and their defined types."

If the types are numeric, then another possibility is that the program/include isn't activated,and the runtime is not the same version as what you see in the editor.

Read only

Muthu_raja
Active Participant
0 Likes
8,087

Hi Anurag,

Is there any Else statement part found for the first if condition ??. Suppose if it is not there and condition fails, It would directly go to the line after the Endif statement of first if condition ( may be in your case line no: 159 )

if lv_flstr ge lv_low and lv_flstr le lv_high.
  read table...
  if
  endif
  if
  endif...
Else <--- search for this


endif.

So please check the Else part is available or not for the first if condition.

Read only

0 Likes
8,087

Hi Muthu,

The code was working fine till yesterday.

It has just come up yesterday evening.

Still we are figuring out the reason for the same.

And also, the else part if added to check if the false reaches to Else or not, but failing to get even there.

Regards,

Anurag Singh

Read only

0 Likes
8,087

Ok I doubt whether the Else part was added recently is corresponding to the first if condition. Ok please confirm once the if condition fails which line it was going ??

Read only

Sandra_Rossi
Active Contributor
8,087

Is it possible that you didn't restart your program ? (i.e. the source code in the debugger refers to the current source code, but the real code executed corresponds the old compiled code/old source code)

Read only

matt
Active Contributor
0 Likes
8,087

This is where my thoughts are going.

Read only

0 Likes
8,087

Hi Sandra,

May be you are right.

But I got this error last evening reported by Client. Then I checked into my system late night and found this again.

Then I came office and checked again in the morning, found the same error in both, Dev and Quality System. So, that possibility is eliminated for now in my case.

Thanks,
Anurag

Read only

anurag_singh16
Participant
8,087

Hello Guys,

Thank you all. And please don't take it as argument. We are helping each other to clear the doubts and there can be a bit of difference in opinions and approach towards problem.

So, let's take it lightly and chill. 🙂 🙂

I cracked it now.

I commented the original statement and split the condition into 2 parts as below.

It is working fine now.

But the question is why was it not working earlier only from past 24 hours. Before yesterday that query was working absolutely fine and accordingly.

Regards,
Anurag

Read only

matt
Active Contributor
8,087
IF lv_flstr GE lv_low AND lv_flstr LE lv_high. 

is logically identical to

IF lv_flstr GE lv_low.
  IF lv_flstr LE lv_high. 

There is absolutely no way that the first doesn't work and the second does, unless you have not given us accurate and complete information. I'd suggest that either you're testing with different data (you still haven't told us the types of the three variables), or you're not showing us the actual code (this happens, it's easy to do).

I'm now rather worried for you. About two things.

1) You posted originally.

IF lv_flstr GE lv_low AND lv_flstr LE lv_high.
  READ ...
  IF sy-subrc = 0.

It looks like you've replaced that with

IF lv_flstr GE lv_low.
  IF lv_flstr LE lv_high.
    IF sy-subrc = 0.

Where's the READ gone?

2) You say you've cracked it, and it's working fine. But that's only half the story. You (and us) still don't understand why it wasn't working before. If don't get that understanding, then you are programming by coincidence, which, if you follow the link I post earlier, you should understand is a very dangerous thing to do.

Read only

8,087

I agree 100% with Matthew

Read only

8,087

Hi Matthew,

Me and my functional both sat together and checked in debugging mode. It worked then, but now again it's creating the problem.

I'll share the details.

Now, the problem is coming at below condition:

1.   IF lv_flstr GE lv_low.   "This was working fine this time around 2 hours back
         IF lv_flstr LE lv_high.  "This was working fine this time around 2 hours back



2.   IF lv_flstr GE lv_low.   "This is working fine now also
         IF lv_flstr LE lv_high.  "But this is creating same problem again i.e., exiting out of code block

Regarding Data Types of all variables in condition statement:

1. lv_flstr  -> char15  
2. lv_low    -> char5
3. lv_high   -> char5
4. lv_flstr1 -> char5

I am fetching 'lv_flstr' after splitting a variable by '-' into lv_low and lv_high.
Earlier lv_flstr was used in condition, then I moved lv_flstr to lv_flstr1 and replaced lv_flstr with lv_flstr1 in  condition.



I guess the problem is from the beginning and data type has to be corrected for all.

Let me know if any further details required.

Regards,
Anurag

Read only

8,087

The type does indeed need to be corrected to a numeric type as Mathew mention previously.

Since for char types 85 > 100 since 8 > 1.

Read only

Former Member
8,087

Hi,

In your latest comment I can see that the reason is created by using character fields in a numerical equation. There is a difference between (x is a space character) between:

12xxx
xxx12
00012

Kind regards, Rob Dielemans

Read only

0 Likes
8,087

anurag.singh16 By the way, better use BETWEEN it's more "functional" (easy to understand), rather than GE and LE :

IF '10' BETWEEN '09' AND '11'. " evaluated as true<br>IF '10' BETWEEN '9' AND '11'. " evaluated as FALSE !!!!

Transfer to numeric variables and that will always work :

IF 10 BETWEEN 9 AND 11. " evaluated as true

(I used literals for clarity, but it will work the same for variables with adequate types)

Read only

matt
Active Contributor
8,087

So, finally we have it. The reason why the IF logic is going to false, is that the operands are chars.

You need to convert them to numeric before comparing, or it simply won't work. Understanding the data types you are working with, and making sure you know what they are, is a key for robust programming.

Read only

DoanManhQuynh
Active Contributor
0 Likes
8,087

There are 2 things you have to care about:

1. You split data from somewhere and put in that variable so you should condese it before compare.

2. As everyone pointed out, its because you comparing character and its not the same as you compare numeric. a slightly change could fix it (see sample below).

p/s: i dont know why you have those variables length difference to each other. you realy want to compare 15 length one with 5 length one?

DATA: lv_flstr TYPE char15,
      lv_low   TYPE char5,
      lv_high  TYPE char5.

lv_flstr = 85.
lv_low = 80.
lv_high = 100.

IF lv_flstr GE lv_low AND lv_flstr LE lv_high. " not work
  BREAK-POINT.
ENDIF.

IF lv_flstr BETWEEN lv_low AND lv_high. " not work
  BREAK-POINT.
ENDIF.

IF CONV i( lv_flstr ) GE CONV i( lv_low ) AND
   CONV i( lv_flstr ) LE CONV i( lv_high ). " work
  BREAK-POINT.
ENDIF.
Read only

anurag_singh16
Participant
0 Likes
8,087

Hi All,

The error was due to data type only. It's fixed now.

The developers didn't think of it and used char5 and char15 instead of numeric data type.

Regards,
Anurag Singh

Read only

matt
Active Contributor
0 Likes
8,087

"Understanding the data types you are working with, and making sure you know what they are, is a key for robust programming."

Maybe you need new developers. 😉