If statement parentheses paradox Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
stealzy
Posts: 91
Joined: 01 Nov 2015, 13:43

If statement parentheses paradox

10 Nov 2022, 06:40

How to explain this behavior? 👽

Code: Select all

If 1 == 1
	MsgBox 1 == 1
If 1 != 1
	MsgBox 1 != 1
	
If (1 == 1)
	MsgBox (1 == 1)
If (1 != 1)
	MsgBox (1 != 1)
Last edited by stealzy on 10 Nov 2022, 06:48, edited 1 time in total.
User avatar
mikeyww
Posts: 27216
Joined: 09 Sep 2014, 18:38

Re: If statement parentheses paradox

10 Nov 2022, 06:44

I believe that with If legacy syntax, the 1 on the left side of the operator is seen as a variable name rather than an expression. See documentation, where it is explained.

The one below is more interesting.

Code: Select all

1 := 1
If 1 = 1
 MsgBox, 64, Result #1, 1 == 1
If 1 == 1
 MsgBox, 64, Result #2, 1 == 1
It's possible that the == cannot be used with legacy If. Others may know more.

I just use expressions in parentheses with If, with only the possible exceptions that the documentation notes.
stealzy
Posts: 91
Joined: 01 Nov 2015, 13:43

Re: If statement parentheses paradox

10 Nov 2022, 06:55

Well, but it not explain this:

Code: Select all

x := 1
1 := 1

If x == 1
	MsgBox x == 1
User avatar
mikeyww
Posts: 27216
Joined: 09 Sep 2014, 18:38

Re: If statement parentheses paradox

10 Nov 2022, 06:56

Yes, my post explains that.

Stick with the parentheses for expressions.
stealzy
Posts: 91
Joined: 01 Nov 2015, 13:43

Re: If statement parentheses paradox

10 Nov 2022, 06:57

mikeyww wrote:
10 Nov 2022, 06:44
I just use expressions in parentheses with If
Same, just curious.
User avatar
mikeyww
Posts: 27216
Joined: 09 Sep 2014, 18:38

Re: If statement parentheses paradox

10 Nov 2022, 07:00

My comment is easily verified.

Code: Select all

x = abc
If x == abc
 MsgBox, Test1
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: If statement parentheses paradox  Topic is solved

10 Nov 2022, 07:30

How to explain this behavior?

Code: Select all

1 := "= 1"
If 1 == 1
   MsgBox "%1%" = "= 1"
User avatar
mikeyww
Posts: 27216
Joined: 09 Sep 2014, 18:38

Re: If statement parentheses paradox

10 Nov 2022, 07:47

Thank you, malcev. That is interesting.

I'd say it shows again that = works in legacy If, whereas == does not (as an operator).
stealzy
Posts: 91
Joined: 01 Nov 2015, 13:43

Re: If statement parentheses paradox

10 Nov 2022, 09:26

Thanks @malcev and @mikeyww for clarification 👍🍻.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot] and 97 guests