If statement not working with OR?

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 4.
To post in this forum you must have a registered copy of Flowcode 4 or higher. To sign up for this forum topic please use the "Online Resources" link in the Flowcode Help Menu.

Moderator: Benj

Post Reply
Werner
Posts: 95
Joined: Sat Jun 11, 2011 4:12 am
Has thanked: 87 times
Been thanked: 31 times
Contact:

If statement not working with OR?

Post by Werner »

Hi Guys, I am trying to use OR in an if Decision. I have: If: Variable1 = 0 OR 1
It does not work for me, any ideas? Thanks for the help!
Werner
Werner
STUDENT OF: Martin - Professor of Flowcode, John, Jan, Fotios and Nicolas "Spanish Dude"

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: If statement not working with OR?

Post by medelec35 »

Hi Werner,

I personally would use:
If: (Variable1 = 0) OR (Variable1 = 1)
You can also use the double pipe '||' for 'OR' byte wise operations. The pipe on my keyboard is just shift & back slash which is next to the z key
E.g
If: (Variable1 = 0) || (Variable1 = 1)

I know there are people who do not use brackets, but the brackets just make sure the actions carried out are what the user intended.

Hope this helps

Martin
Martin

User avatar
Steve
Matrix Staff
Posts: 3421
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: If statement not working with OR?

Post by Steve »

In this context, "OR" is inappropriate. "OR" equates to the C code operator "|" which is a bitwise-OR operation (e.g. 5 OR 12 = 13, because 5=0b0101 and 12=0b1100, giving 0b1101 = 13).

You want the "||" operator which is a logical-OR operation - e.g. if ( 5 > 8 ) || ( 2 < 3 ) which equals "true".

I fully agree with Martin on the use of brackets - they should be used to make your intention more clear.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: If statement not working with OR?

Post by medelec35 »

medelec35 wrote: You can also use the double pipe '||' for 'OR' byte wise operations.
Thanks Steve,
I was only referring to bytewise operations not bitwise, as I thought (probably wrongly) bytewise was the type of variable required.
I do agree that OR is bitwise but within Flowcode it also works with bytes, and that’s what I have always used instead of || to compare a variable with value 1 or value 2
Perhaps I have been wrong by using OR, but it works as I intended.

Martin
Attachments
OR.fcf
(5 KiB) Downloaded 280 times
Martin

User avatar
JonnyW
Posts: 1230
Joined: Fri Oct 29, 2010 9:13 am
Location: Matrix Multimedia Ltd
Has thanked: 63 times
Been thanked: 290 times
Contact:

Re: If statement not working with OR?

Post by JonnyW »

Morning.

Both operators can be used on any integer values, regardless of the bit size of that value. The reason why OR works as expected in the conditional is C (and Flowcode) treat 'true' as 'not false', therefore any non-zero value is true. For conditionals it is not usually as efficient, however, and it is usually better to use '||'.

There are several differences between OR ('|') and logical-or ('||'), here are some:

Logical-or will evaluate the result to 'true' or 'false', bitwise evaluates to the bitwise value:

Code: Select all

  (5 || 6) == 1
  (5 | 6) == 7
(Though just to confuse matters further I have seen optimised compilers that evaluate '(5 || 6) == 5' when the result isnt stored in a variable!)

Logical-or will not necessarily execute the entire expression, and will exit the condition when the result is guaranteed to be true, so:

Code: Select all

  if (1 || complex_expression)
Will not execute complex_expression as there is no point. Bitwise-or will execute this, as it will effect the result. This makes it execute faster in most situations.

Logical-or has a guaranteed execution order (left to right), where as bitwise-OR has no guarantee and is compiler/platform dependent, so:

Code: Select all

  is_function_valid || call_function()
Will only execute call_function() if it is valid, where as:

Code: Select all

  is_function_valid | call_function()
Will not only execute the function regardless, but may even call it before is_function_valid is even processed. I believe the BoostC compiler works this way (it does with addition, at any rate).

The operator precedence is slightly different, but this isnt a concern for most operations, and brackets shouldnt be required for simple comparison expressions (though they should be provided for clarity).

I hope any of this is of use,

Jonny

dbasnett
Posts: 125
Joined: Mon Aug 15, 2011 1:54 pm
Has thanked: 8 times
Been thanked: 11 times
Contact:

Re: If statement not working with OR?

Post by dbasnett »

I wrote a little test program using OR and ||. The if's were simple

if (foo=1) OR (foo=2)
if (foo=1) || (foo=2)

and when compiled the generated C code was different. However, to my untrained eye, the generated ASM looked identical.

User avatar
JonnyW
Posts: 1230
Joined: Fri Oct 29, 2010 9:13 am
Location: Matrix Multimedia Ltd
Has thanked: 63 times
Been thanked: 290 times
Contact:

Re: If statement not working with OR?

Post by JonnyW »

Hi. Yeah, that may well be the case, depends on the compiler and the context its used in.

I added a C code icon that did the following and the lines produced very different ASM (though my PIC8 assembly eye is pretty untrained itself):

Code: Select all

int A, B;
A = (A | B);
A = (A || B);
However, the generated C/ASM code is passed through an optimiser. This looks at the assembly generated and decides if the generated code is the most efficient that can be produced. If the compiler decides that its actually better to treat '||' as '|', and there are no side effects to the operation, it may well optimise these to be the same ASM code. By side effects, the compiler will almost certainly not do this optimisation if:
  • * The right-hand expression dereferences memory (array accesses, etc)
    * The right-hand expression calls a function (that could do anything)
    * The right hand expression assigns a value to a variable
    * The result of the operation is assigned to a variable, where || should only be 0 or 1
This means that the code:

Code: Select all

if (index >= array_max || data[index] == invalid) // Then bad index
Is valid and always safe, but this equivalent is not:

Code: Select all

if (index >= array_max | data[index] == invalid) // Then bad index
As it will always access the 'data' array even if 'index' is out of bounds.

As a general rule it is better to use '||' when you only care the result is either true or false (as in if statements). It is better to use '|' when you need to know the actual result of the operation, like in bitmasking:

Code: Select all

if (A || B)    // Result is true or false
    C = D | E; // Result is an integer with a range of values
Jonny

Werner
Posts: 95
Joined: Sat Jun 11, 2011 4:12 am
Has thanked: 87 times
Been thanked: 31 times
Contact:

Re: If statement not working with OR?

Post by Werner »

Thanks Guys!! Worked like magic! How are you supposed to know this stuff? Nowhere in the Flowcode help file does it mention ||. :D
Werner
STUDENT OF: Martin - Professor of Flowcode, John, Jan, Fotios and Nicolas "Spanish Dude"

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: If statement not working with OR?

Post by medelec35 »

Steve wrote:In this context, "OR" is inappropriate. "OR" equates to the C code operator "|" which is a bitwise-OR operation (e.g. 5 OR 12 = 13, because 5=0b0101 and 12=0b1100, giving 0b1101 = 13).
Steve, would you mind clearing something up for me please?

A quote from the recommended Microcontroller systems Engineering by Bert van DAM

page 162
Note the use of brackets: (data < 32) OR (data > 126). It is highly recommended
......etc
That is showing the use of (var = byte) OR (var = byte) that I posted is correct format.

Not saying you are wrong since you know far more than I do, but is the book wrong that agrees with me?

Maybe from what you and Jonny are saying the book should of stated:

Code: Select all

Note the use of brackets: (data < 32) || (data > 126). It is highly recommended.........
?

Martin
Martin

User avatar
JonnyW
Posts: 1230
Joined: Fri Oct 29, 2010 9:13 am
Location: Matrix Multimedia Ltd
Has thanked: 63 times
Been thanked: 290 times
Contact:

Re: If statement not working with OR?

Post by JonnyW »

Hi Martin.

I am not famillar with the book, but I assume either this is a book based around C or BASIC, or the example is pseudo-code and not specific to a language. Either way, 'OR' isnt a C keyword or operator, and I assume this book would be generalising in this case as to correct use of brackets, not use of the OR operator.

The 'OR' used in Flowcode is specific to Flowcode syntax and is functionally identical to the C '|'. This is the same as the BASIC 'OR' keyword - BASIC does not have an equivalent (that I know of) for the C '||'. The same goes for 'AND', '&' and '&&'.

What I mean is if you put:

Code: Select all

A = B OR C
In a calculation icon, this will work OK, but if you put it in a C icon (assuming the correct variables and semi-colon) it will fail to compile. The same is true for the '<>' operator for 'not equal' and all Flowcode textual operators (AND, OR, MOD, etc). There may be other syntax or features like this that are supported in Flowcode but not C - I have the Flowcode grammar at work and would have to check.

Jonny

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: If statement not working with OR?

Post by medelec35 »

JonnyW wrote:Hi Martin.

I am not famillar with the book, but I assume either this is a book based around C or BASIC, or the example is pseudo-code and not specific to a language.
Hi Jonny, only reason book is mentioned is because it's based around Flowcode, and is sold by Matrix Multimedia and Elektor.
The (data < 32) OR (data > 126) is used in an actual flowchart, and not part of c code.
take a look at:
http://www.matrixmultimedia.com/mmforum ... 80&p=11087

My point is I stated in my first reply you can use If: (Variable1 = 0) OR (Variable1 = 1). I was told that's wrong which is fair enough.
But the book about Flowcode shows on a flowchart
If: (data < 32) OR (data > 126)

So the question is: is the book wrong as well (the only difference is > and < is used instead of =)
If so then I will correct it and submit a typo report.
Or is
If: (data < 32) OR (data > 126)
correct but
If: (data = 32) OR (data = 126) would be wrong?

To be honest I have always used the format of If: (variable1 = variable2) OR (variable1 = variable3) in all my flowcharts for both bytes and int, and has worked 100% of the time.

Martin
Martin

User avatar
Steve
Matrix Staff
Posts: 3421
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: If statement not working with OR?

Post by Steve »

I would definitely see this as an error in the book, because the intention of the code is wrong. It might work in practice, but if this is by accident rather than design then I would see this as a problem.

So, rather than use this "bitwise" operation:

Code: Select all

(data < 32) OR (data > 126)
I would strongly recommend that you use this "logical" operation, because this is what your intention actually is:

Code: Select all

(data < 32) || (data > 126)

Post Reply