Weird if statement error

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
squadjot
Posts: 39
Joined: 17 Nov 2024, 08:55

Weird if statement error

Post by squadjot » 14 Dec 2024, 08:15

I was debugging some if statements and discovered an oddity.

Every way of writing the if statement below is valid, except for the last one

Code: Select all

myFunc(){
	return "yep"
}

istrue := true
;----------------------------------
if ( istrue ) alsotrue := "also true"
;----------------------------------
if ( istrue ) confirm := myFunc() 
;----------------------------------
if ( istrue ) myFunc()  ; <-- this gives error (Unexpected "if")
Isn't that a bit weird?, or is it that how it is supposed to be?

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

Re: Weird if statement error

Post by mikeyww » 14 Dec 2024, 08:26

It is not a bit weird. It is supposed to be as the documentation describes it.
If the If statement's expression evaluates to true (which is any result other than an empty string or the number 0), the line or block underneath it is executed. Otherwise, if there is a corresponding Else statement, execution jumps to the line or block underneath it.
Source: If - Syntax & Usage | AutoHotkey v2

Code: Select all

#Requires AutoHotkey 2
istrue := True
If ( istrue ) myFunc() = '1yep'
 MsgBox

myFunc(){
 Return 'yep'
}

Code: Select all

#Requires AutoHotkey 2
alsotrue := "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
If f := ( False ) alsotrue := "A new assignment"
 MsgBox f
Last edited by mikeyww on 14 Dec 2024, 08:33, edited 2 times in total.

User avatar
gregster
Posts: 9252
Joined: 30 Sep 2013, 06:48

Re: Weird if statement error

Post by gregster » 14 Dec 2024, 08:28

Afaics, you get this error only because no other statement is following the last If - but it would be required. The first two Ifs have another If-line that will execute conditionally.
You can simply confirm this by adding a line containing msgbox at the end or just by changing the order of If-lines. Then you will get the same error for the new last line without a following statement.

Memo:
If wrote:If the If statement's expression evaluates to true (which is any result other than an empty string or the number 0), the line or block underneath it is executed.
Edit: Again, too late. ;)
Last edited by gregster on 14 Dec 2024, 08:35, edited 1 time in total.

squadjot
Posts: 39
Joined: 17 Nov 2024, 08:55

Re: Weird if statement error

Post by squadjot » 14 Dec 2024, 08:32

Ohh... so it's only because it's the last line. ..doooh.
Ok that was a bit of an edge case, but nice to know. Thanks

So now this is ok.

Code: Select all

myFunc(){
	return "yep"
}

istrue := true
;----------------------------------
if ( istrue ) alsotrue := "also true"
;----------------------------------
if ( istrue ) confirm := myFunc() 
;----------------------------------
if ( istrue ) myFunc()
;----------------------------------
something := ""

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

Re: Weird if statement error

Post by mikeyww » 14 Dec 2024, 08:35

  1. Your assignments via := always occur in the script that you posted, because they are not subject to If.
  2. The entire expression following If on the same line is evaluated to determine whether the conditional statement is met. Parentheses may be used in the expression, but the expression is the entire line.
  3. The conditional statement or block must reside on the line or lines underneath the If statement (line).

squadjot
Posts: 39
Joined: 17 Nov 2024, 08:55

Re: Weird if statement error

Post by squadjot » 14 Dec 2024, 08:45

"the expression is the entire line"..oh ok

I guess this is illustrated here

Code: Select all

is_false := false

if( is_false ) MsgBox( "was true" ) ; this will show -->  "was true"

if( is_false ) {
  	MsgBox( "was true" )
}else {
	MsgBox( "was false" ) ; this will show  -->  "was false"	
}

test := ""

User avatar
gregster
Posts: 9252
Joined: 30 Sep 2013, 06:48

Re: Weird if statement error

Post by gregster » 14 Dec 2024, 08:50

The result of the expression doesn't depend on what the msgbox shows, but on its return value. Msgbox() will usually return a string like OK, Cancel, Yes, ... depending on which button the user pressed.
Compare https://www.autohotkey.com/docs/v2/lib/MsgBox.htm#Result

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

Re: Weird if statement error

Post by mikeyww » 14 Dec 2024, 08:54

Another illustration is below.

Code: Select all

#Requires AutoHotkey 2
MsgBox MsgBox(MsgBox('xyz')), 'The second value returned'

squadjot
Posts: 39
Joined: 17 Nov 2024, 08:55

Re: Weird if statement error

Post by squadjot » 14 Dec 2024, 09:02

Yes, ok. Coming from javascript, this is definitely something to be aware of. Thank you.

squadjot
Posts: 39
Joined: 17 Nov 2024, 08:55

Re: Weird if statement error

Post by squadjot » 14 Dec 2024, 09:05

I guess theres no syntax like this either? (or similar?)

Code: Select all

maybetrue ? doA() : doB()
or

Code: Select all

wastrue := maybetrue ? "yes" : "no"

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

Re: Weird if statement error

Post by mikeyww » 14 Dec 2024, 09:07

The documentation is a great source of information for you.

https://www.autohotkey.com/docs/v2/Variables.htm#ternary

squadjot
Posts: 39
Joined: 17 Nov 2024, 08:55

Re: Weird if statement error

Post by squadjot » 14 Dec 2024, 09:24

It's not like i haven't looked for it.. :D The docs are great, but things are not always easy to find. Especially if you're looking for something that is not there ( Edit : Correction, if you don't know what the thing you're looking for is called 😅 )
Last edited by squadjot on 14 Dec 2024, 09:50, edited 1 time in total.

squadjot
Posts: 39
Joined: 17 Nov 2024, 08:55

Re: Weird if statement error

Post by squadjot » 14 Dec 2024, 09:47

Oh yes, thanks for the direct link, that's what i need

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

Re: Weird if statement error

Post by mikeyww » 14 Dec 2024, 10:24

In case you do not know where to find the item in the documentation, you can look it up in the index.
  1. Go to the documentation.
  2. Select the "Index" tab at the top.
  3. Type what you would like to find. An example is "?".

User avatar
flyingDman
Posts: 3072
Joined: 29 Sep 2013, 19:01

Re: Weird if statement error

Post by flyingDman » 14 Dec 2024, 10:35

squadjot wrote:
14 Dec 2024, 09:05
I guess theres no syntax like this either?
It does not help if you start exploring AHK with prejudices...
The fact that things are different does not make them weird.

If you had spent 1 minute reading the docs, you would have had all the answers you need.
The responsiveness of members of this forum is well known. Don't abuse it.
14.3 & 1.3.7

squadjot
Posts: 39
Joined: 17 Nov 2024, 08:55

Re: Weird if statement error

Post by squadjot » 14 Dec 2024, 16:10

mikeyww wrote:
14 Dec 2024, 10:24
In case you do not know where to find the item in the documentation, you can look it up in the index.
  1. Go to the documentation.
  2. Select the "Index" tab at the top.
  3. Type what you would like to find. An example is "?".
Thanks, i was using "Search" not the "Index"

Post Reply

Return to “Ask for Help (v2)”