Curious if IfEqual Has a Bug Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
thomastthai
Posts: 18
Joined: 12 Mar 2020, 01:51

Curious if IfEqual Has a Bug

12 May 2020, 19:10

I understand that IfEqual is deprecated. The reason I'm bringing this up is that I ran into codes from the forum from years ago. I'm trying to understand it.

The code showed IfEqual command used with another command following on the same line. The line after that was indented. It looked as though the proceeding line would be executed if the IfEqual condition was true right along with the command that follows on the same line.

According to the manual:

IfEqual Manual
Note that command-like If statements allow a command or command-like control flow statement to be written on the same line, but misspelled command names are treated as literal text.
Here are three test cases with comments in the code that showed expected and unexpected behaviors:

Code: Select all

; Case 1
; Test IfEqual executes the next line when another command follows 
; on the same line and the if condition is true.

x := 1

; Since EnvAdd is a command, it should be executed 
IfEqual, x, 1, EnvAdd, x, 1
	x += 10

MsgBox % "Case 1a: Expecting x to be 12. x is " . x ; x is 12

; ------------------------------------------------------------------------------
; Case 2
; Test IfEqual executes the next line when another command follows 
; on the same line and the if condition is false.

x := 1

; Since EnvAdd is a command, it should be executed 
IfEqual, x, 2, EnvAdd, x, 1 ; <== condition should fail since x is 1
	x += 10 ; <== Does this run if the above condition fails? It seem like it does!

MsgBox % "Case 2: Expecting x to be 1. x is " . x ; x is 11

; ------------------------------------------------------------------------------
; Case 3
; Test IfEqual executes the next line when FUNCTION, not command, follows 
; on the same line and the if condition is true.

x := 1

; Since XPlusOne is a funtion, not a command, IfEqual shouldn't execute it
IfEqual, x, 1, XPlusOne(x) 
	x += 1 ; Why does this not get executed if x is equal to 1? Is it because XPlusOne() isn't a command?

; Expecting the print out to show "Case 2: x is 2"
; BUT it prints out "Case 2: x is 1"
MsgBox % "Case 3: Expecting x to be 2. x is " . x ; x is 1 

ChangeY(ByRef x)
{ 
	x += 10
}

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

Re: Curious if IfEqual Has a Bug

12 May 2020, 19:49

Cases 1 and 2 act as expected. Whether or not the line below the IfEqual line being indented was thought to mean it's conditional when the IfEqual line has an inline command, the fact is that it is not conditional on it. Only the inline command is. To go over cases 1 and 2 in detail:
  • Case 1: The condition is true, so it adds 1 making x = 2. Then the following line is executed no matter what happened above, so x = 12.
  • Case 2: The condition is false, so it doesn't add 1, leaving x = 1. Then the following line is executed no matter what happened above, so x = 11.
Regarding case 3, apparently having a misspelled inline command causes nothing to to be executed on the following line whether the condition was true or not. It's not especially troubling since there is a obvious error in the place where the inline command should be, so that needs to be corrected before there could be any execution that would be "expected." It's basically an error condition.
thomastthai
Posts: 18
Joined: 12 Mar 2020, 01:51

Re: Curious if IfEqual Has a Bug

13 May 2020, 12:38

Hi @boiler! Thanks for the reply.

To check my understanding: for IfEqual, if another command follows on the SAME line, the next line is processed regardless of what happens.

Following the logic of cases 1 and 2, shouldn't the following line get executed regardless if the condition is true or false for case 3?
User avatar
boiler
Posts: 17404
Joined: 21 Dec 2014, 02:44

Re: Curious if IfEqual Has a Bug  Topic is solved

13 May 2020, 14:57

thomastthai wrote: To check my understanding: for IfEqual, if another command follows on the SAME line, the next line is processed regardless of what happens.
Yes, the only conditionally executed statement if you put a command on the same line is that command on the same line. Subsequent lines are not conditional.
thomastthai wrote: Following the logic of cases 1 and 2, shouldn't the following line get executed regardless if the condition is true or false for case 3?
One might think that may be the case, but like I said, apparently it doesn't execute the following line when the inline command is not an actual command, so it's an error condition where there really is no expected behavior because the error needs to be fixed anyway.
thomastthai
Posts: 18
Joined: 12 Mar 2020, 01:51

Re: Curious if IfEqual Has a Bug

14 May 2020, 17:04

Thank you @boiler for clarifying. I'm learning.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 356 guests