Normal Topic Precedence Alert (Read 822 times)
MP
Full Member
***
Offline



Posts: 104
Joined: Sep 3rd, 2007
Precedence Alert
Nov 21st, 2007 at 6:19pm
Print Post Print Post  
In 2.0.3 I put the following code into the On Element Entry event of a command button, fully expecting nothing to happen when I clicked the button:

Code
Select All
If NOT False AND NOT True Then
	WriteLn( "Why am I here?" )

If (NOT False) AND (NOT True) Then
	WriteLn( "I am here." ) 


Much to my surprise, "Why am I here?" was dumped to the Slate, proving once again the value of the old mantra "When in doubt, use parentheses."
  
Back to top
 
IP Logged
 
nelsona
Member
*
Offline


No personal text

Posts: 10
Joined: Jan 10th, 2006
Re: Precedence Alert
Reply #1 - Nov 30th, 2007 at 11:25pm
Print Post Print Post  
I quickly looked at snipets and  by what I can tell, those  two statements should be equivalent
The order of operations  would have the not expressions be evaluated first and then the and statement

Since you are inverting the boolean expressions,  the and statement should always eval to false.
In my head I can see what sesame is really doing is this :
Code
Select All
If NOT (False AND NOT True) Then
	WriteLn( "Why am I here?" ) 



It appears that sesame isn't following order of operations for whatever reason and is just going straight across the expression
I dusted off the sesame programming manual and it says that it evals not before and expressions

Most of these don't apply to sesame obviously but should show how the order of operations should go:
Simple Assignment Operator

    =      Simple assignment operator

Arithmetic Operators

    +      Additive operator (also used for String concatenation)
    -      Subtraction operator
    *      Multiplication operator
    /      Division operator
    %      Remainder operator

Unary Operators

    +      Unary plus operator; indicates positive value (numbers are positive without this, however)
    -      Unary minus operator; negates an expression
    ++        Increment operator; increments a value by 1
    --          Decrement operator; decrements a value by 1
    !          Logical compliment operator; inverts the value of a boolean

Equality and Relational Operators

    ==      Equal to
    !=      Not equal to
    >      Greater than
    >=      Greater than or equal to
    <      Less than
    <=      Less than or equal to

Conditional Operators

    &&      Conditional-AND
    ||      Conditional-OR
    ?:      Ternary (shorthand for if-then-else statement)

Type Comparison Operator

    instanceof      Compares an object to a specified type

Bitwise and Bit Shift Operators

    ~      Unary bitwise complement
    <<      Signed left shift
    >>      Signed right shift
    >>>      Unsigned right shift
    &      Bitwise AND
    ^      Bitwise exclusive OR
    |      Bitwise inclusive OR


as parenthesis, they should be used to make the code clearer and easier to follow, just blindly adding them when in doubt isn't a good practice to follow.
  
Back to top
 
IP Logged
 
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Re: Precedence Alert
Reply #2 - Dec 1st, 2007 at 12:05am
Print Post Print Post  
nelsona wrote on Nov 30th, 2007 at 11:25pm:
as parenthesis, they should be used to make the code clearer and easier to follow, just blindly adding them when in doubt isn't a good practice to follow.


They should always be used around every expression in every programming language that supports them - always. That way the programmer is explicit and exact with the compiler as to the order they expect, in every single case.

Letting the compile decide is always a bad idea.

SBasic supports the "usual" (though not universal) order of operations for mathematic operations. All others are undetermined and should always be explicitely set by the programmer using parenthesis. Math operations should also always be braced in parenthesis, even if they do default to the order you expect.

Again, this is good practice in any programming language - not just SBasic.
  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
Back to top
IP Logged