Multiple commands under an 'if' statement Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
roysubs
Posts: 421
Joined: 29 Sep 2018, 16:37

Multiple commands under an 'if' statement

Post by roysubs » 10 Apr 2020, 05:01

Some questions on the Python-like indent option with Autohotkey.

Take this example:

Code: Select all

MsgBox, 4,, Would you like to continue? (press Yes or No)
IfMsgBox Yes
    MsgBox You pressed Yes.
To do more under the 'IfMsgBox Yes' line, I have to do:

Code: Select all

MsgBox, 4,, Would you like to continue? (press Yes or No)
IfMsgBox Yes
{
    MsgBox You pressed Yes.
    x := 123
}
Or is there an alternative, can I do more under the 'IfMsgBox Yes' line without the { } ?

Code: Select all

MsgBox, 4,, Would you like to continue? (press Yes or No)
IfMsgBox Yes
    MsgBox You pressed Yes. <some divider that says next command> x := 123 <divider> y := 234
In PowerShell for example, they use ";" to have multiple commands on one line:

Code: Select all

$x = 0 ; $y = 1 ; $z = 2

User avatar
Scr1pter
Posts: 1271
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Multiple commands under an 'if' statement

Post by Scr1pter » 10 Apr 2020, 05:20

I believe the answer is no.

However, you could add just a function under
IfMsgBoxYes.

Inside of your function you place your code lines.

Cheers!
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09

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

Re: Multiple commands under an 'if' statement  Topic is solved

Post by just me » 10 Apr 2020, 05:27

Comma (multi-statement), but in general it can be used only for expressions/functions, not for commands.

roysubs
Posts: 421
Joined: 29 Sep 2018, 16:37

Re: Multiple commands under an 'if' statement

Post by roysubs » 10 Apr 2020, 06:30

Both really useful, thanks for the clarifications.

rzjnzk
Posts: 2
Joined: 23 Nov 2017, 09:45
Contact:

Re: Multiple commands under an 'if' statement

Post by rzjnzk » 18 Jan 2022, 07:38

Maybe this?

Code: Select all

MsgBox, 4, Would you like to continue? (press Yes or No)
IfMsgBox No
    Return
MsgBox You pressed Yes.
x := 123
Last edited by rzjnzk on 21 Jan 2022, 01:55, edited 1 time in total.

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

Re: Multiple commands under an 'if' statement

Post by boiler » 18 Jan 2022, 08:41

rzjnzk wrote: Maybe this?
```
MsgBox, 4,, Would you like to continue? (press Yes or No)
IfMsgBox No
{
return
}
MsgBox You pressed Yes.
x := 123
```
The braces aren’t necessary, but more importantly, that only works for the case where there is nothing else to be executed in the script/function/subroutine after this.

Post Reply

Return to “Ask for Help (v1)”