Code: Select all
var := 0
for k, v in ["var"] {
if %v% ; %v% = var = 0
MsgBox, % %v% ; MsgBox should not appear
}
Code: Select all
var := 0
for k, v in ["var"] {
if %v% ; %v% = var = 0
MsgBox, % %v% ; MsgBox should not appear
}
Code: Select all
var := 0
for k, v in ["var"] {
if (%v%) ; %v% = var = 0
MsgBox, % %v% ; MsgBox should not appear
}
Code: Select all
var := 0
v := "var"
if %v%
MsgBox, % %v%
Code: Select all
var := 0
v := "var"
if %v% + 0
MsgBox, % %v%
Code: Select all
var := 0
for k, v in ["var"] {
if %v% ; %v% = var = 0
MsgBox(%v%) ; MsgBox should not appear
}
Code: Select all
var := 0
for k, v in ["var"] {
if !!%v% ; %v% = var = 0
MsgBox(%v%) ; MsgBox should not appear
}
Code: Select all
q:: ;test legacy if
var := 1
if %var%
MsgBox, % "y"
else
MsgBox, % "n"
var := 0
if %var%
MsgBox, % "y"
else
MsgBox, % "n"
return
Code: Select all
var := 0
v := "var"
if %v% + 0
MsgBox 1
if %v% + 1
MsgBox 2
Code: Select all
IfEqual, %var% + 1 ; there is no variable with name "0 + 1"
MsgBox 2
Code: Select all
;legacy if-statements:
if var
if %var%
if a = b ;treated as: if (a = "b") [= is one of the 7 operators: = <> != < <= > >=]
;note:
if a == b ;treated as: if (a = "= b") [== is not one of the 7 operators]
Your syntax is incorrect and the behavior thus undefined.An if-statement that contains an expression is differentiated from a traditional-if such as If FoundColor <> Blue by making the character after the word "if" an open-parenthesis. Although this is usually accomplished by enclosing the entire expression in parentheses, it can also be done with something like if (x > 0) and (y > 0). In addition, the open-parenthesis may be omitted entirely if the first item after the word "if" is a function call or an operator such as "not" or "!".
The % operator of a single (or first) if argument is always a variable reference, or address. It's best illustrated this way:The == operator behaves identically to = except when either of the inputs is not a number, in which case == is always case sensitive and = is always case insensitive
Code: Select all
if MyVar = %MyVar%
MsgBox The contents of MyVar agree with themselves!
Code: Select all
if %MyVar% = %MyVar%
MsgBox This message won't appear!
So if %var% shouldn't be traditional if, but rather if expression since the first item after the word if is an operator. That being said, it seems %% isn't consider an operator such as the others though, this should be clarified, for example something like if %var%v = 1, does traditional if. In addition, if expression page should either mention that if %var% and if (var) are functionally identical, similar to the remarks on the return expression page, if there is a reason for this, eg., backwards compatibility, if there is no reason for this behaviour, I would call it a bug (although I highly doubt this is isn't intended).if (expresssion) wrote: An if-statement that contains an expression is differentiated from a traditional-if such as If FoundColor <> Blue by making the character after the word "if" an open-parenthesis. [...] In addition, the open-parenthesis may be omitted entirely if the first item after the word "if" is a function call or an operator such as "not" or "!".
Which you can read as, this is a total mess, just put () around your if expressions and don't even bother to try to get the logic of this. That is what I did in v1, it worked.If Statement wrote:Common cause of confusion: There are several other types of If statements, some of which look very similar to If (expression). These should be avoided in new scripts. If in doubt, it is best to always begin the expression with an open-parenthesis.
That part of the documentation shows some ways that the distinction can be made, not the rules of the distinction (feel free to improve the documentation). There is actually one fundamental rule:Helgef wrote:The rules of the distinction between traditional if and if expression are documented as, [...]
Any If statement which does not match one of the usages shown above is interpreted as If (expression).
Source: Scripting Language | AutoHotkey
%var% is valid within any InputVar parameter, including the Var on the left side of legacy If statements (and the entire parameter for the simple If Var statement).Helgef wrote:So if %var% shouldn't be traditional if, but rather if expression since the first item after the word if is an operator.
That was clear, hence my comments, That being said....That part of the documentation shows some ways that the distinction can be made
Even with the fundamental rule, the documentation is incomplete, or there is a bug, sinceThere is actually one fundamental rule:
Code: Select all
if %var% { ; OTB -> if (expression)
}
Code: Select all
if var {
}
I literally contradicted you. If it was truly clear, you could have avoided writing inaccurate statements.Helgef wrote:That was clear, hence my comments,
Excellent .The missing link is...
Users browsing this forum: finecobra and 5 guests