Macro Express Forums

# Execution order of AND + OR

## Recommended Posts

The title says it, which are executed first in complex If loops, AND or OR? Or are they executed in command list order?

##### Share on other sites

Command order.

.......................................................................

Simply evaluate the first pair and note the truth. Then take that and evaluate it and the 3rd, take the truth of that and evaluate with the 4th, and so on.

##### Share on other sites

Thanks, so it's by command list order. It's so difficult to follow large amounts of logic. I had four blocks, each consisting of a number of If, And, Or. Trying to follow the logic was impossible through all four blocks. I just wanted to put each block in brackets so each was executed as an entity. I ended up setting a variable from each block and then adding logic to handle those. Ultimately unnecessary but made visualizing and troubleshooting easier.

##### Share on other sites

The first time I had trouble with it I tried the sequential pairs order and I must have messed something up because it appeared that was not the logic. So I replicated my logic in a more simple test macro and ran a matrix of all possible variable combinations against it and had MEP create a results table. Much easier than manually testing. Turns out I had something funky with a variable in the first go.

The real fun is when one has a complex set of conditions and figuring out what order and which conjunctions to use.

##### Share on other sites

This subject has given me headaches since your post. I've been trying to figure out if the order really matters. When I was looking at my blocks of logic, the function that joined them certainly needed some thought, AND or OR. Mentally you have to put the blocks in brackets. There is no way I could start programming at the first line of the 1st block and finish at the end of the 4th block one line at a time, even if that is how they are executed.

##### Share on other sites

This subject has given me headaches since your post. I've been trying to figure out if the order really matters. When I was looking at my blocks of logic, the function that joined them certainly needed some thought, AND or OR. Mentally you have to put the blocks in brackets. There is no way I could start programming at the first line of the 1st block and finish at the end of the 4th block one line at a time, even if that is how they are executed.

Then I certainly advise you to avoid APL (A Programming Language). Originally created as a notation methodology for mathematicians, it parses its syntax in the order received, but from right to left (unless brackets intervene)! So 12 = 2 x 4 + 2 is true.

##### Share on other sites

This subject has given me headaches since your post. I've been trying to figure out if the order really matters. When I was looking at my blocks of logic, the function that joined them certainly needed some thought, AND or OR. Mentally you have to put the blocks in brackets. There is no way I could start programming at the first line of the 1st block and finish at the end of the 4th block one line at a time, even if that is how they are executed.

John: Any chance of showing the script, or a similarly ambiguous example? It sounds like the same mental block I sometimes get with ME Pro's logic script commands. But I usually put that down to the decades that have passed since I did any programming (if you exclude a little VBS in Word and Excel). I recall from early in my IBM career that such sections typically followed a 'block-diagramming' stage which helped get the thinking straight.

Paul: Come to think, wasn't it IBM that invented APL?

--

##### Share on other sites

I'm sure Paul or Cory have far more complex logic but anyway, here's one of the 4 blocks (I could not find it until I realized it was in ME3). To separate visually I add a full-width Comment between each block. So that I could troubleshoot, instead of ANDing the blocks I set text outputs and performed the ANDing with those.

If Variable %N11% = 23

AND

If Variable %N1% = 0

OR

If Variable %N1% >= variable %N11%

Variable Set String %T40% "run1"

End If

The question was, if you did all the ANDs first then the ORs would it make a difference compared to starting top to bottom. Cory pointed out that it was executed top to bottom. When you think about it, it would be hard to do it any other way because logic truth may depend on knowing the results of previous logic. In my example the value of T40 must be known before you can resolve its ANDed use further down.

The above logic was time/date-related. The original logic was more complex because I was using AM/PM versus 24hour clock. Such a pain in ME3 but so much easier in MEPro with decimal time.

Re IBM, they were the guys that back in the day said there would only be a market for 5 electronic computers.

##### Share on other sites

This stuff never fails to get my head spinning. I think the rule is to evaluate each command in order. That may be saying exactly the same as Cory in different words!

Your specific example was relatively straightforward. I interpreted it first by what I'd call 'common sense observation'. IOW, what I thought you were trying to do.

[(If N11=23) AND (If N1=0)] OR (If N1>=N11) then set T40 to "run1"

Nevertheless I had to run the following macro several times before I could be confident enough to post this!

Repeat Start (Repeat 100 times)

Variable Set String %T[40]% to ""

Variable Set Integer %N[11]%: Prompt

Variable Set Integer %N[1]%: Prompt

If Variable %N[11]% Equals "23"

And

If Variable %N[1]% Equals "0"

Or

If Variable %N[1]% Is Greater Than or Equal To "%N[11]%"

Variable Set String %T[40]% to "run1"

End If

Text Box Display: Results

End Repeat

```<REPEAT START Start="1" Step="1" Count="100" Save="TRUE" Variable="Run"/>
<VARIABLE SET STRING Option="\x00" Destination="%T[40]%"/>
<VARIABLE SET INTEGER Option="\x01" Destination="%N[11]%" Prompt="Enter a value for N[11]." Mask="FALSE" OnTop="FALSE" Left="Center" Top="Center" Monitor="0"/>
<VARIABLE SET INTEGER Option="\x01" Destination="%N[1]%" Prompt="Enter a value for N[1]." Mask="FALSE" OnTop="FALSE" Left="Center" Top="Center" Monitor="0"/>
<IF VARIABLE Variable="%N[11]%" Condition="\x00" Value="23" IgnoreCase="FALSE"/>
<AND/>
<IF VARIABLE Variable="%N[1]%" Condition="\x00" Value="0" IgnoreCase="FALSE"/>
<OR/>
<IF VARIABLE Variable="%N[1]%" Condition="\x04" Value="%N[11]%" IgnoreCase="FALSE"/>
<VARIABLE SET STRING Option="\x00" Destination="%T[40]%" Value="run1"/>
<END IF/>
<TEXT BOX DISPLAY Title="Results" Content="{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang2057{\\fonttbl{\\f0\\fnil Tahoma;}{\\f1\\fnil\\fcharset0 Tahoma;}}\r\n{\\colortbl ;\\red0\\green0\\blue255;}\r\n\\viewkind4\\uc1\\pard\\cf1\\b\\f0\\fs16 Run\\f1  = \\f0 %Run%\\cf0\\b0 \r\n\\par \r\n\\par N[1]\\f1  = \\f0 %N[1]%\r\n\\par N[1\\f1 1\\f0 ]\\f1  = \\f0 %N[1\\f1 1\\f0 ]%\r\n\\par T[40]\\f1  = \\f0 %T[40]%\r\n\\par }\r\n" Left="Center" Top="Center" Width="278" Height="200" Monitor="0" OnTop="FALSE" Keep_Focus="TRUE" Mode="\x00" Delay="0"/>
<END REPEAT/>
```

I'm sure there must be a more intuitive way to express complex logic sequences, but I'm darned if I know how.

--

##### Share on other sites

I'm sure there must be a more intuitive way to express complex logic sequences, but I'm darned if I know how.

I think this code looks a bit clearer:

```If Variable %n[11]% Equals "23"
If Variable %n[1]% Equals "0"
Variable Set String %t[40]% to "run1"
End If
Else
If Variable %n[1]% Is Greater Than "%n[11]%"
Variable Set String %t[40]% to "run1"
End If
End If```

Here it is more obvious that the condition n1 > n11 is independent of the other conditions.

Or you could try this:

```Switch( %n[11]% )
Case: 23
If Variable %n[1]% Equals "0"
Variable Set String %t[40]% to "run1"
End If
End Case
Default Case
If Variable %n[1]% Is Greater Than "%n[11]%"
Variable Set String %t[40]% to "run1"
End If
End Case
End Switch```

##### Share on other sites

Thanks Paul, indentation does help a lot.

I'd been thinking about trying Case, I'll study your example tomorrow. (18 February 2010, 23:23 here.)

--

##### Share on other sites

"I'm sure there must be a more intuitive way to express complex logic sequences, but I'm darned if I know how"

You're right. In complex industrial design you don't create logic with computer code. You use logic diagrams (using AND, OR, NAND, NOR gates etc). Even a simple latch (equivalent to a relay latching in using a momentary pushbutton) is so much easier to visualize. In that case the output is fed back into the input to hold it in. Where you have thousands of pieces of logic interconnected it's the way to go. Once the design is done, you can convert to whatever programming you are using (eg computer code or discrete electronic logic gates). Flow charts are basically the same thing.

You can achieve the same thing in computer code by compartmentalizing per my logic and Terry's but it's so much more difficult. I'm sure it gets easier with practice. I wonder if Paul's code is something that you would arrive at during design. Most of us think in intuitive terms as Terry mentioned which is not going to give the most elegant mathematical solution. Perhaps that's the difference; the plebs would be happy with working code, Paul&Co would tidy it up!

##### Share on other sites

"I'm sure there must be a more intuitive way to express complex logic sequences, but I'm darned if I know how"

You're right. In complex industrial design you don't create logic with computer code. You use logic diagrams (using AND, OR, NAND, NOR gates etc). Even a simple latch (equivalent to a relay latching in using a momentary pushbutton) is so much easier to visualize. In that case the output is fed back into the input to hold it in. Where you have thousands of pieces of logic interconnected it's the way to go. Once the design is done, you can convert to whatever programming you are using (eg computer code or discrete electronic logic gates). Flow charts are basically the same thing.

You can achieve the same thing in computer code by compartmentalizing per my logic and Terry's but it's so much more difficult. I'm sure it gets easier with practice. I wonder if Paul's code is something that you would arrive at during design. Most of us think in intuitive terms as Terry mentioned which is not going to give the most elegant mathematical solution. Perhaps that's the difference; the plebs would be happy with working code, Paul&Co would tidy it up!

This thread reminds me of my first program I wrote before the dinosaurs were destroyed!

I wrote this IF statement in COBOL, which went to 9 levels of nesting and occupied a printed page and a half.

The astonishing thing was that it worked almost first time after much testing.

But I eventually paid the price. Some 6 months later I was required to alter some of the logic... and I simply couldn't do it! In the end, I had to rewrite the logic into something much more digestible.

##### Share on other sites

"I'm sure there must be a more intuitive way to express complex logic sequences, but I'm darned if I know how"

You're right. In complex industrial design you don't create logic with computer code. You use logic diagrams (using AND, OR, NAND, NOR gates etc). Even a simple latch (equivalent to a relay latching in using a momentary pushbutton) is so much easier to visualize. In that case the output is fed back into the input to hold it in. Where you have thousands of pieces of logic interconnected it's the way to go. Once the design is done, you can convert to whatever programming you are using (eg computer code or discrete electronic logic gates). Flow charts are basically the same thing.

You can achieve the same thing in computer code by compartmentalizing per my logic and Terry's but it's so much more difficult. I'm sure it gets easier with practice. I wonder if Paul's code is something that you would arrive at during design. Most of us think in intuitive terms as Terry mentioned which is not going to give the most elegant mathematical solution. Perhaps that's the difference; the plebs would be happy with working code, Paul&Co would tidy it up!

That could be fertile ground for further discussion. Practical electronics was once an obsessive hobby of mine so your post had me dusting off my 'Finished Circuits' binders. Scanning through those projects that required some logic testing, they're 'methodically modular' in design. I'd try to start with a plain english expression of the requirement, and sketch out a block diagram. Except in the most trivial projects there would invariably be several 'independent tests' to consider, just as there are in business applications.

1. Are any of the following 3 normally closed burglar alarm switches open: front door, kitchen door, garage window?

2. Has the exit delay finished?

3. Has the panic button been pressed?

4. Has the PIR detector been activated at least twice within 10 seconds ?

etc

I'd first deal with each of these separately. (Even these individual modules could be difficult to understand later without good documentation, to echo Paul's point. My earlier hand-drawn circuits are now largely incomprehensible to me. 'Obvious' symbols are now obscure puzzles!) The bottom line for each was a High or Low signal (a True or False). But with each intermediate step's output identified in case further need for it emerged. If my grasp of the overall design was good before I started the detail circuit design (script coding in ME terms) then I'd probably be sure that I didn't need to identify these intermediate steps. But even then I'd get hopelessly lost without working in a modular way.

#1 above would generate a circuit like this:

So one approach to complex ME logic coding might be to first break it down to a set of variables, all of which must be True to proceed to the next step. As variable names can be meaningful (IncomeOverThreshold, AgeUnder16, AllTaxPaid...) this should at least make it much more readable. Pedestrian, but methodical, and with a chance that the macro could be understood a month later. Even looking at Paul's indented version again this morning, I'm still not grasping its meaning without careful thought.

--

##### Share on other sites

I had a look on the Net for logic diagrams but the information seems overly technical, by introducing too many gates styles, truth tables and other logic representations. Wiki is so-so. As far as ME goes we are mostly interested in AND and OR. Many companies use the standard symbols but others simplify. A gate is a vertical line and midway vertical on the right is a small touching circle containing AND, OR etc. Some would have a square for an AND and a circle for an OR. This style uses less space and is easier to have many inputs. The logic flow is left to right although signals can loop back. When going from one page to another, the signal description ("at least one entry switch activated") and destination are indicated ("to audible alarm").

You don't show power like electrical logic. Logic simply has values of 0 or 1. Any "1" input on an OR gate will set the output to "1". For an AND gate every input has to be "1". You can substitute TRUE and FALSE if that is easier to understand. If the physical inputs were switches, they would usually give a "1" when closed (but that could be a maintained "1" like a light switch or a momentary "1" like a pushbutton).

Using your alarm system, if you had 10 window switches you want the alarm to go off if any one of them activates. So you have an OR gate with 10 inputs. However, you want the alarm to stay on if the window closes. So what you do is loop the output back to the (11th) input. Once the output is on, "1", the gate will stay on permanently. In practice you would have some logic to break the feedback to turn it off.

That would translate to logic:

IF %switch1% = 1

OR

IF %switch2% = 1

OR

IF %switch[n]% = 1

OR

If %output% = 1 - once output is on it holds itself in

(AND

IF %reset switch% = 0 - pressing the reset breaks the logic)

END IF

Variable Set %output% = 1

For non-discrete logic >= GT, EQ etc you can have boxes with the symbol in it. I could not find a good reference anywhere. (edit) This link has a few examples that are too small to read well.

For the sort of things we've been talking about you probably would not do more than a rough sketch on paper or think it out in your head. For normal tasks we make decisions every day, "If the price is between these two values and it's after pay day and if any one of the 4 stores is selling it"

For the flow charts style see this link this link or this. This may be a better method for the average person as it has far more common usage.

##### Share on other sites

Take a look at State Machines.

##### Share on other sites

I'm not sure why you're so confused by this.

............................................

Just do it in order. Let's consider your lines in the example. Read line 1 and replace it in your mind with true or false. Read line 3 and decide if it's true or false. Now if both 1 AND 3 are true replace lines 1-3 with True in your mind, else false. Now evaluate line 5 and mentally replace it with a true or false. Since line 4 is an OR if either the result of the 1-3 eval OR line 5 is true mentally replace all of lines 1-5 with True, else false. And just plow thru. If it's hard to visualize print it out, write the values into all the variable positions then start crossing each one out and replacing them with T of Fs like I described.

Obviously order can make a difference. In math we have order of operations. A scientific calculator knows how to perform these in order. A standard calculator performs them in order. But as a young lad I could still use my crappy old TI (remember the ones before LCDs with the crappy reg segmented displays?). Now I could do the calculations in chunks writing down each value (no memory in that calc) but I was too lazy and would just mentally rearrange the formula so I could do is sequentially. This is what I do with MEP.

But Paul is correct that in some cases it's more elegant to nest conditions but depending on what you’re trying to do this could create more lines and possibly duplication of lines of code. It all depends on the situation.

For hundreds thousands of years philosophers (logic being a doctrine), linguists and now even psychologists have argued where logical though is patterned after speech or vice versa. But what is agreed is that they are like electricity and magnetism and one cannot be without the other. So we can logically talk thru any logic. A little while ago I wrote a RSS downloader for my Vikings game torrents. To figure it out just talk it thru. So my RSS macro would grab the title from the XML, size and something else I forget. But just say it like you were explaining to someone what you wanted. If the title contains "Minnesota" OR "Vikings" AND its size is less than 3GB download it. There's your order.

##### Share on other sites

From my experience when somebody compresses code, for example using arrays and repeat loops, it's a great solution providing some documentation is provided to explain how it is set up. In the case of Paul's compressed logic, some simple added comments on the purpose of the logic block help no end. The same applies to my original logic, except that my logic is more in line with normal thought (as both Cory and I gave examples of) and would generally be easier to follow. I've forgotten the logic myself already but it's something like "Around midnight 01.00 is later than 23.00 even though 1 < 23".

Way back I pushed for a "Comment" icon to be added to the Editor (not sure if one appeared because of that) to make it easier to document the purpose of sections of commands. To me it's the hallmark of consideration for subsequent users of the macro, including yourself six months from now.

##### Share on other sites

From my experience when somebody compresses code, for example using arrays and repeat loops, it's a great solution providing some documentation is provided to explain how it is set up. In the case of Paul's compressed logic, some simple added comments on the purpose of the logic block help no end. The same applies to my original logic, except that my logic is more in line with normal thought (as both Cory and I gave examples of) and would generally be easier to follow. I've forgotten the logic myself already but it's something like "Around midnight 01.00 is later than 23.00 even though 1 < 23".

I disagree. "Normal thought" is often unclear or badly articulated. Given that most of us are familiar with the concept of operator precedence, e.g. AND often takes a higher priority than OR, then "normal" thought may be ambiguous, depending on how it is expressed, and on whether there is a hidden assumption that AND takes precedence over OR.

Consider simple arithmetic. What does "normal" thought understand by this?

2 x 3 + 4

Is the answer 10 or 14, and why?

##### Share on other sites

Aren't we almost back to my original question? With mathematics, unless you truly understand the order of operation, you put things in brackets to ensure they follow your rules. My example would be the familiar 5*(T1-32)/9. In ME this is not an issue. Maths is very cumbersome, one operation at a time with an effective "=" each line. You can evaluate top downwards, albeit rather tediously. Your 2x3+4 will be 10 if programmed in that order T1*T2=T3 T3+T4=T5. OK, I know you would use T1*T2=T2 T2+T3=T3 !! So would I if I did not have to re-use them.

In logic, the order of comparisons in consecutive ORs does not matter. The order of ANDed comparisons doesn't matter either providing they are still together. In ME, since it is working top to bottom, you can figure out how it will be evaluated - "Truth as You Go". Any "brackets" are either mental or per my output "run1" at the end of a section of logic.

In my logic, the setting of "run1", which is intended to indicate a separation of logic blocks, appears twice in your code thus diluting its purpose.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×