If statement not working with OR?
Moderator: Benj
-
- 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?
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
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"
STUDENT OF: Martin - Professor of Flowcode, John, Jan, Fotios and Nicolas "Spanish Dude"
-
- 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?
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
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
- Steve
- Matrix Staff
- Posts: 3422
- Joined: Tue Jan 03, 2006 3:59 pm
- Has thanked: 114 times
- Been thanked: 422 times
- Contact:
Re: If statement not working with OR?
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.
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.
-
- 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?
Thanks Steve,medelec35 wrote: You can also use the double pipe '||' for 'OR' byte wise operations.
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 281 times
Martin
- 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?
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:
(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:
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:
Will only execute call_function() if it is valid, where as:
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
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
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)
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()
Code: Select all
is_function_valid | call_function()
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
-
- 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?
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.
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.
- 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?
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):
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:
Is valid and always safe, but this equivalent is not:
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:
Jonny
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);
- * 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
Code: Select all
if (index >= array_max || data[index] == invalid) // Then bad index
Code: Select all
if (index >= array_max | data[index] == invalid) // Then bad index
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
-
- 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?
Thanks Guys!! Worked like magic! How are you supposed to know this stuff? Nowhere in the Flowcode help file does it mention ||.
Werner
STUDENT OF: Martin - Professor of Flowcode, John, Jan, Fotios and Nicolas "Spanish Dude"
STUDENT OF: Martin - Professor of Flowcode, John, Jan, Fotios and Nicolas "Spanish Dude"
-
- 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?
Steve, would you mind clearing something up for me please?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).
A quote from the recommended Microcontroller systems Engineering by Bert van DAM
page 162
......etcNote the use of brackets: (data < 32) OR (data > 126). It is highly recommended
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
- 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?
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:
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
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
Jonny
-
- 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?
Hi Jonny, only reason book is mentioned is because it's based around Flowcode, and is sold by Matrix Multimedia and Elektor.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.
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
- Steve
- Matrix Staff
- Posts: 3422
- Joined: Tue Jan 03, 2006 3:59 pm
- Has thanked: 114 times
- Been thanked: 422 times
- Contact:
Re: If statement not working with OR?
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:
I would strongly recommend that you use this "logical" operation, because this is what your intention actually is:
So, rather than use this "bitwise" operation:
Code: Select all
(data < 32) OR (data > 126)
Code: Select all
(data < 32) || (data > 126)