Making IfElse statement do nothing

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ry3ks3u_m4dd0ns
Posts: 18
Joined: 29 Mar 2017, 09:23

Making IfElse statement do nothing

02 Apr 2017, 08:59

This is semi-related to my last topic, which was compressing if statements.

So, I've done that, and now I've tried to make an if-else statement that needs to do nothing.

More specifically, if PixelGetColor gets 9F2A2A, which is sort of a bit darker red, I need to add 1 to a variable called RedCount, and if it's not the color I need, it simply does nothing, other than stay in the loop.

This is bound to a hotkey, and I've tried Return, but there's another set of hotkeys below it, so yeah. It ran those.

Current code I have:

Code: Select all

SetTimer, RedCounter, 20

RedCounter:
PixelGetColor, currentsym, 50, 50 , RGB //directed to the middle, since the window gets filled with the color code it generates.
If currentsym = 0x9F2A2A 
{
	some random action
} else {
	return //this is where I dont know what to do
}
User avatar
Nextron
Posts: 1391
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: Making IfElse statement do nothing

02 Apr 2017, 10:25

Just remove everything after and including else.
User avatar
tidbit
Posts: 1273
Joined: 29 Sep 2013, 17:15
Location: USA

Re: Making IfElse statement do nothing

02 Apr 2017, 10:35

IF does not require an ELSE.
ELSE does require an IF.

don't want an "else"? Or you want "else carry on with what it's doing, like a loop"? simply don't add an else.
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
User avatar
JoeWinograd
Posts: 2214
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Making IfElse statement do nothing

02 Apr 2017, 10:36

If you want nothing to be done when the If-statement's expression evaluates to False, don't have an Else-statement — simply do a Return. The lack of a Return is why the hotkeys below it run — even when the If-statement's expression evaluates to True. So you want something like this:

Code: Select all

RedCounter:
PixelGetColor, currentsym, 50, 50 , RGB
If currentsym = 0x9F2A2A
{
  code for True here
}
Return
If you insist on having an Else clause, the code below will work, but there's no reason for it that I can see:

Code: Select all

RedCounter:
PixelGetColor, currentsym, 50, 50 , RGB
If currentsym = 0x9F2A2A
{
  code for True here
  Return
}
Else
{
  code for False here - could be no code
  Return
}
Regards, Joe
User avatar
tidbit
Posts: 1273
Joined: 29 Sep 2013, 17:15
Location: USA

Re: Making IfElse statement do nothing

02 Apr 2017, 10:39

JoeWinograd wrote:If you want nothing to be done when the If-statement's expression evaluates to False, don't have an Else-statement — simply do a Return.
Nope. a RETURN is not needed to "do nothing". he also said he wants in in a loop, which using a RETURN would end the loop, which he doesn't want to do.
He is even using RETURN in his example code. The same way you suggested.
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Making IfElse statement do nothing

02 Apr 2017, 11:18

He probably just needs to remove the word 'return', because 'return' in that line ends the subroutine.

I think this is an interesting question, because sometimes when I'm writing code, I do an if/else statement, and I want one part to temporarily do nothing. I might use 'Sleep 0', but is there a better 'nothing-y' command to use? Otherwise I could use curly brackets, but that uses up 2 lines.

Code: Select all

;q:: ;much ado about doing nothing

;use Sleep 0
Loop, 5
{
	WinGetClass, vWinClass, A
	if (vWinClass = "Notepad")
		ToolTip, % "is notepad " A_Now
	else
		Sleep 0
	Sleep 1000
}

;use curly brackets
Loop, 5
{
	WinGetClass, vWinClass, A
	if (vWinClass = "Notepad")
		ToolTip, % "is notepad " A_Now
	else
		{
		}
	Sleep 1000
}

;you don't actually need the 'else' command in this example
Loop, 5
{
	WinGetClass, vWinClass, A
	if (vWinClass = "Notepad")
		ToolTip, % "is notepad " A_Now
	Sleep 1000
}

ToolTip
MsgBox, % "done"
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
ry3ks3u_m4dd0ns
Posts: 18
Joined: 29 Mar 2017, 09:23

Re: Making IfElse statement do nothing

02 Apr 2017, 11:35

Thank you all for the responses, I will try them all with my current script, and I'll reply back with my progress.
User avatar
tidbit
Posts: 1273
Joined: 29 Sep 2013, 17:15
Location: USA

Re: Making IfElse statement do nothing

02 Apr 2017, 15:35

jeeswg wrote:but is there a better 'nothing-y' command to use? Otherwise I could use curly brackets, but that uses up 2 lines.
some languages have something like "doNothing" or "pass", that does nothing, just allows you to make an if/else a little easier to read.
"if this is true, do nothing, else do this"
But ahk does not

but in this topics case, he can just remove the entire else section as he doesn't want it and it's not needed. so why add something?
rawr. fear me.
*poke*
Is it December 21, 2012 yet?

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], peter_ahk and 375 guests