If, and statement

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
TrebleTA
Posts: 134
Joined: 20 Nov 2021, 06:44

If, and statement

Post by TrebleTA » 23 May 2022, 09:38

Why is it that if i have say..

Code: Select all

aaa=1
bbb=2
If aaa=1 and bbb=2
I do the above yet even tho bbb=2 its not registering, so i am having to do it like this,

Code: Select all

if aaa=1
{
         if bbb=2
        {
         }
}
was wondering why?

User avatar
boiler
Posts: 16962
Joined: 21 Dec 2014, 02:44

Re: If, and statement

Post by boiler » 23 May 2022, 09:40

Code: Select all

If (aaa=1) and (bbb=2)
It's because you are using the legacy If statement rather than the If (expression) statement.

User avatar
mikeyww
Posts: 26944
Joined: 09 Sep 2014, 18:38

Re: If, and statement

Post by mikeyww » 23 May 2022, 09:41

I was writing as boiler was.

Code: Select all

aaa=1
bbb=2
c=1 and bbb=2
d := "1 and bbb=2"
If (aaa=1 and bbb=2)
 Send x
If d=1 and bbb=2
 Send y
Explained: IfLegacy If patterns
Last edited by mikeyww on 23 May 2022, 09:42, edited 1 time in total.

just me
Posts: 9458
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: If, and statement

Post by just me » 23 May 2022, 09:42

Code: Select all

If (aaa=1) and (bbb=2)
:arrow: If (Expression)

TrebleTA
Posts: 134
Joined: 20 Nov 2021, 06:44

Re: If, and statement

Post by TrebleTA » 23 May 2022, 10:47

Thanks so i was missing the () in the if statement.

also this next statement

Code: Select all

Process, Exist, this process has spaces.exe
Should it be as there is spaces

Code: Select all

Process, Exist, "this process has spaces.exe"

TrebleTA
Posts: 134
Joined: 20 Nov 2021, 06:44

Re: If, and statement

Post by TrebleTA » 23 May 2022, 10:50

also say the if statement if its just say if aaa=1 is that ok or should that is also

Code: Select all

if (aaa=1)

User avatar
mikeyww
Posts: 26944
Joined: 09 Sep 2014, 18:38

Re: If, and statement

Post by mikeyww » 23 May 2022, 10:54

Try it. See if it works. That is the beauty. You can test it without anyone's permission!

TrebleTA
Posts: 134
Joined: 20 Nov 2021, 06:44

Re: If, and statement

Post by TrebleTA » 23 May 2022, 11:08

the if statement well i was doing like

Code: Select all

if  !errorlevel & winexist(ahk_exe this) & winexist(ahk_exe thisb) & aaa =1 or !errorlevel & winexist(ahk_exe check2) & winexist(ahk_exe check2) & aaa=1
But the aaa was not getting noticed.
I now have its like

Code: Select all

if  !errorlevel
{
	if  winexist(ahk_exe this) & winexist(ahk_exe thisb) or !errorlevel & winexist(ahk_exe check2) & winexist(ahk_exe check2)
	{
		if aaa=1
		{
		run, my task or process's
		}
	}
}
but as someone has said, its the () I was missing, Thank you. T
The other. I just wanting to know if its the correct way to have " " if its linked to like program files x86
or as i linked above?

User avatar
boiler
Posts: 16962
Joined: 21 Dec 2014, 02:44

Re: If, and statement

Post by boiler » 23 May 2022, 11:14

& is not the shorthand version of logical and. It is the bit-wise "and" that you use for choosing 0s and 1s (will be 1 only if both are 1) at a byte/bit level. The shorthand for logical and is &&.

Where did you see that you are supposed to put quotes around a process name if it has spaces in the Process, Exist command? I haven't seen that and it doesn't make sense that it would be needed.

TrebleTA
Posts: 134
Joined: 20 Nov 2021, 06:44

Re: If, and statement

Post by TrebleTA » 23 May 2022, 11:15

or even should it be ("this process has spaces.exe")

TrebleTA
Posts: 134
Joined: 20 Nov 2021, 06:44

Re: If, and statement

Post by TrebleTA » 23 May 2022, 11:17

thanks for the reply well i have this.

Code: Select all

	Process, Exist, Intelligent standby list cleaner ISLC.exe
	If !ErrorLevel																	; If the task is NOT running
But its not detecting it is running
p.s thanks for the update with the & in my script i do use and, but its good to know the correct way. and & alone being wrong
Last edited by TrebleTA on 23 May 2022, 11:21, edited 1 time in total.

User avatar
boiler
Posts: 16962
Joined: 21 Dec 2014, 02:44

Re: If, and statement

Post by boiler » 23 May 2022, 11:18

Regarding ("..."), I think you are confusing where you see this in the documentation:

Code: Select all

DllCall("GetCurrentProcessId")

And that doesn't have to do with whether there are spaces or not. When parentheses are used in situations like that, it's because it's a function call, and the parameter is an expression. And with expressions, literal strings are quoted. The process name parameter in the Process command is not an expression.

TrebleTA
Posts: 134
Joined: 20 Nov 2021, 06:44

Re: If, and statement

Post by TrebleTA » 23 May 2022, 11:22

so why is it not always detecting the process is running?
sorry for the edit, i have others that detect fine its just this one process that seems to get missed somehow why i was thinking were am i going wrong

User avatar
boiler
Posts: 16962
Joined: 21 Dec 2014, 02:44

Re: If, and statement

Post by boiler » 23 May 2022, 11:27

Just confirmed that quotation marks are not only not needed when there are spaces in the process name, but it will not work if they are included.

Are you sure you have the name of the process correct? Can you show it in Task Manager?

User avatar
boiler
Posts: 16962
Joined: 21 Dec 2014, 02:44

Re: If, and statement

Post by boiler » 23 May 2022, 11:32

You have a problem with your statements like this:

Code: Select all

if  !errorlevel & winexist(ahk_exe this) & winexist(ahk_exe thisb) & aaa =1 or !errorlevel & winexist(ahk_exe check2) & winexist(ahk_exe check2) & aaa=1

You need quotation marks inside the parameters to function calls because they are expressions and you are passing literal strings. So winexist(ahk_exe this) must be winexist("ahk_exe this"). Unless this is a variable containing the process name, then it would be winexist("ahk_exe " this).

Also, you should check the logic of your compound statements with multiple ands (remember to change them to &&) and an or. You may need to be grouping various parts using parentheses.

RussF
Posts: 1269
Joined: 05 Aug 2021, 06:36

Re: If, and statement

Post by RussF » 23 May 2022, 11:36

TrebleTA wrote:

Code: Select all

if  !errorlevel & winexist(ahk_exe this) & winexist(ahk_exe thisb) & aaa =1 or !errorlevel & winexist(ahk_exe check2) & winexist(ahk_exe check2) & aaa=1
could be:

Code: Select all

if (!errorlevel && aaa=1) && ((winexist(ahk_exe this) && winexist(ahk_exe thisb)) || winexist(ahk_exe check2))
You have winexist(ahk_exe check2) checked twice.

Russ

Edit: Oops, forgot an "&" - thanks @mikeyww!
Last edited by RussF on 23 May 2022, 11:43, edited 1 time in total.

User avatar
mikeyww
Posts: 26944
Joined: 09 Sep 2014, 18:38

Re: If, and statement

Post by mikeyww » 23 May 2022, 11:40

Using bitwise instead of logical AND for WinExist can lead to an unexpected result, because WinExist returns a HWND.

User avatar
boiler
Posts: 16962
Joined: 21 Dec 2014, 02:44

Re: If, and statement

Post by boiler » 23 May 2022, 11:43

I believe some things are in there twice because it's two groupings of a series of "and"s that are separated by an "or". And it is desired that that part be true in either of the "or" segments. So I think it's actually meant to be in there twice for good reason. The things you took out of one side probably need to be true in that other side as well. To show the grouping with parentheses:

Code: Select all

if  (!errorlevel && winexist("ahk_exe this") && winexist("ahk_exe thisb") && aaa =1) or (!errorlevel && winexist("ahk_exe check2") && winexist("ahk_exe check2") && aaa=1)
(well, actually, the extra check2 part could come out, but I can see why !errorlevel and aaa=1 is in there twice)

RussF
Posts: 1269
Joined: 05 Aug 2021, 06:36

Re: If, and statement

Post by RussF » 23 May 2022, 11:44

My post has been edited. Thanks @mikeyww!

Russ

User avatar
boiler
Posts: 16962
Joined: 21 Dec 2014, 02:44

Re: If, and statement

Post by boiler » 23 May 2022, 11:48

And if the intended grouping is as I indicated with parentheses, then the parentheses are not needed because of order of precedence of the operators, as this demonstrates (result is 1):

Code: Select all

MsgBox, % 1 && 1 || 0 && 1
(actually, I don't think that's a definitive demonstration, but I think the point is still true)

Post Reply

Return to “Ask for Help (v1)”