Strange behaviour of If / Else If / Else / End If (Solved)

fmcpma

Member


Hello all.

I seem to have found one very peculiar behaviour of the If … construct. I noticed it in a much larger (and more useful, I hope ;-) macro but I seem to have tracked it down to just the following (if I remove the code in the sub-sections, this doesn't happen):

If_-_Else_If_-_Else_-_End_If.png

Now, what happens is this: If the macro is run as it is, both the If in section A and the Else If in Section B are executed, when, of course, only section A should. (Please ignore the sub-sections for now. Also, the dialogues have the Block next actions… option checked, so that none of them is missed.) If the condition in the Else If in section B is changed to a = 1, then sections A and C are executed, which is also wrong as again only A should.

As I said, the code in the sub-sections, though it doesn't do anything, must be there for either of the two errors mentioned above to appear; otherwise, the If construct functions normally. In A1, as far as I could find – and I spent a few good hours messing about with all possibilities – the Else If condition must be a = x, where x is any value but 0. In C1, the If / Else If / End If must be there for the errors to occur, but the values variable a is compared with don't matter.

Can anyone figure this one out?
 
Last edited:

Endercraft

Moderator (& bug finder :D)
I think I figured it out. I will include screenshots as well so it makes more sense.

What I think is happening, is that the sub section A1 is interfering. I'm not sure of the other sub section (C1) but I think the issue works fine without them.
(I just tested and the issue does indeed work without C1.)

According to what I saw in the system log (use the detailed mode for stuff like that), the problem comes from A1. As you can see in the following screenshot, even though the actions are supposed to stop at action 3, it's not the case.
Screenshot_2023-04-10-10-38-26-460-edit_com.arlosoft.macrodroid.jpgImportant to note how MacroDroid action are stored and executed. They're like arrays.
Screenshot_2023-04-10-10-36-45-996-edit_com.arlosoft.macrodroid.jpgAnd as I just realized, it's not because the last action is an End If action, or because it skipped directly onto the Else If (which means what I wrote just before is half wrong), but because as a condition failed before reaching the end of actions, MacroDroid thinks the first If (A) has failed,so it tries the Else If even if it's not supposed to. Then if the End If is false, then the Else will trigger.
Screenshot_2023-04-10-10-52-08-312-edit_com.arlosoft.macrodroid.jpg
If anyone thinks that's not the problem feel free to point it out.
 

Qarboz

Well-known member
I think I figured it out. I will include screenshots as well so it makes more sense.

What I think is happening, is that the sub section A1 is interfering. I'm not sure of the other sub section (C1) but I think the issue works fine without them.
(I just tested and the issue does indeed work without C1.)

According to what I saw in the system log (use the detailed mode for stuff like that), the problem comes from A1. As you can see in the following screenshot, even though the actions are supposed to stop at action 3, it's not the case.
View attachment 5240Important to note how MacroDroid action are stored and executed. They're like arrays.
View attachment 5239And as I just realized, it's not because the last action is an End If action, or because it skipped directly onto the Else If (which means what I wrote just before is half wrong), but because as a condition failed before reaching the end of actions, MacroDroid thinks the first If (A) has failed,so it tries the Else If even if it's not supposed to. Then if the End If is false, then the Else will trigger.
View attachment 5236
If anyone thinks that's not the problem feel free to point it out.
Your step 4 is different from that of @fmcpma, the latter uses the same condition of step 0 (a=0), which I personally don't understand


Edit
I saw after you wrote a comment in the screenshot...
 

Endercraft

Moderator (& bug finder :D)
Your step 4 is different from that of @fmcpma, the latter uses the same condition of step 0 (a=0), which I personally don't understand


Edit
I saw after you wrote a comment in the screenshot...
Yes I think it was an error when rewriting this sub section again. But as I wrote it doesn't matter.
 

fmcpma

Member
It seems Endercraft is on the right track and the If in A1 is messing up the outer If.

I must also add that, for me, the problem doesn't manifest itself if I cut out section C1, as I originally explained. I just tried it out again and same result: no C1, no problem. I even rewrote the whole macro from scratch without C1 just now, in case the original version was somehow corrupted, and no problem; I then added C1 and got the problem back. Maybe it's the build being used; mine is v5.32.8 Pro, the one currently in Play Store. I think these differences might be important.

Anyway, I'll just officialy report this as a bug.
 

Endercraft

Moderator (& bug finder :D)
It seems Endercraft is on the right track and the If in A1 is messing up the outer If.

I must also add that, for me, the problem doesn't manifest itself if I cut out section C1, as I originally explained. I just tried it out again and same result: no C1, no problem. I even rewrote the whole macro from scratch without C1 just now, in case the original version was somehow corrupted, and no problem; I then added C1 and got the problem back. Maybe it's the build being used; mine is v5.32.8 Pro, the one currently in Play Store. I think these differences might be important.

Anyway, I'll just officialy report this as a bug.
Ah yes it is necessary indeed. I disabled it so I thought it didn't make a difference, but when deleting it the issue doesn't happen. How odd...

I would also like to thank you for being so clear when describing the issue in detail. That way we don't have to spend a month understanding 😊
 

fmcpma

Member
Odd indeed. And If, of course, is at the foundation of every macro – every program in every language, for that matter.

I just hope MacroDroidDev can fix it when he comes back from his vacation ;-)
 

dhj49er

Well-known member
I'm still trying to understand the use case for this "issue".
I don't know when you would use the same condition on an if statement and the following else if statement.
In all my uses of if & else if, the conditions are different, never the same, and I don't see the reported issue.
 

fmcpma

Member
I'm still trying to understand the use case for this "issue".
I don't know when you would use the same condition on an if statement and the following else if statement.
In all my uses of if & else if, the conditions are different, never the same, and I don't see the reported issue.

First of all, the issue first ocurred to me in a real, long macro, which I then simplified as much as I could, while still having the bug occur, until I arrived at the listing here, where it is more or less obvious what is causing the erroneous behaviour, at least in part, as Endercraft pointed out. For the record, in my original, functional macro, an Else executes when it shouldn't.

Thing is, the If… construct is a cornerstone of every programming language. As you know, only one of its branches is ever allowed to execute; if more than one do execute, using whatever conditions and even if those conditions are all equal, then one has a broken programming environment, no matter if one has already encountered the issue or not. Workarounds and avoidances are no solution.

Also, perhaps you have missed the part in my original explanation of the bug where I say that if the conditition in the Else If is changed to a = 1, then the Else If is not executed but the Else is – in addition to the If, that is. In this case, the condition in the If statement and the one in the following Else If statement are not equal and yet again more than one branch execute.

I'm sure Jamie won't mind if I include here his answer to my reporting of this bug:

Thanks for this bug report, I'm currently away on holiday but I just tried this and totally agree this is quite broken.​
I will fix it for the next update.​
Thanks,​
Jamie​
 
Last edited:
Top