Re: Syntax for OR
- Posted by _tom (admin) Mar 04, 2015
- 1619 views
It seems that this syntax is supported ?
if seqConn[1] = 1 or 3 then
and is equivalent to..
if seqConn[1] = 1 or seqConn[1] = 3 then
casey
The way to resolve a syntax question is to write some code!
- Simplify: seqConn[1] could be an object; test with atoms first
- Execute: write some working code
-- test by runnning different values -- | -- v atom sc = 2 if sc = 1 or 3 then puts(1, "found" ) else puts(1, "not found" ) end if --> found -- not the intended result
What is going on?
Order of operations means sc = 1 is evaluated first. The result of this snippet may be either 0 (false) or 1 (true).
Next you evaluate one of two possible remaining expressions. You need to refer to logic true tables for this:
0 or 3 -- true 1 or 3 -- true
The "surprise" is that you always get true.
atom sc = 2 if sc = 1 or sc = 3 then puts(1, "found" ) else puts(1, "not found" ) end if --> not found
This code makes more sense.
atom sc = 3 if sc = ( 1 or 3 ) then puts(1, "found" ) else puts(1, "not found" ) end if --> not found
The parentheses get evaluated first:
1 or 3 -- true
Next you evaluate
sc = 1
Since 3 does not equal 1 you get "not found" which is not the "intended" result. The "trap" in this example that sc=1 works as intended and all other values work as intended.
If you think outside of the box, as Pete does, you end up with:
atom sc = 2 if find( sc, {1,3} ) then puts(1, "found" ) else puts(1, "not found" ) end if --> not found
This works as "intended."
An alternative approach:
atom sc = 3 switch sc do case 1, 3 then puts(1, "found" ) case else puts(1, "not found" ) end switch --> found
Rant... always answer a question with working code.
_tom