I'm confused about Not's precedence

Holzchopf

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jul 31, 2017
Messages
500
Code:
If Not 1=2 Then Print "SHOULDN'T THIS BE TRUE?"

won't print the text. Why? Because "Not 1" evaluates to 0 first, then "0=2" is evaluated, which is false.

Am I getting old or was this always like that in basic-like languages? I'm really confused :confused:
 

Phil7

Administrator
CX Code Contributor
3rd Party Tool Dev
Joined
Jun 26, 2017
Messages
735
Maybe it is just parentheses. Not (1=2)
The question is what is "Not 1"

Should have read properly. Not 1 evaluates False
 
Last edited:

MikeHart

Administrator
CX Code Contributor
3rd Party Module Dev
3rd Party Target Dev
3rd Party Tool Dev
Joined
Jun 19, 2017
Messages
3,500
Yes, always use parenthesis around an expression which you want to negate.
 

Holzchopf

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jul 31, 2017
Messages
500
I guess I only used Not in statements like If Not obj Or obj.state = STATE_DEAD Then where this doesn't strike since it's evaluating the boolean value of obj (e.g. null/not null). I couldn't explain why else I never knew about the precedence of Not. Of course in numerical comparisons I used to use <>, but since these two symbols are hard to type on my laptop keyboard (*facepalm*), I typed Not instead. Learned a thing. At least :)
 

Goodlookinguy

New member
3rd Party Module Dev
Joined
Nov 14, 2017
Messages
11
I think understanding the precedence of the bool not is easier to understand in some other languages because of the way it's usually written, let's take a generic C-style language for instance...

PHP:
if (!1 == 2) print("THIS WILL NEVER BE TRUE!");

When written like this it makes it clearer that 1 is negated before comparison in my opinion. Someone should write up a precedence chart for Monkey. Just copy this one https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence and switch it out with Cerberus' stuff.
 

Holzchopf

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jul 31, 2017
Messages
500
Sounds like a good idea.

Maybe the Operators article in the docs is already precedence-ordered. Ah yes, there it is; stated in the footnote of the table. I didn't expect to find the precedence chart under Expressions. A separate entry (in the docs outline) would be nice.
 

Phil7

Administrator
CX Code Contributor
3rd Party Tool Dev
Joined
Jun 26, 2017
Messages
735
In my opinion the whole language reference should be split into seperate pages for each topic. Too much scrolling..
 
Top Bottom