I understand people have asked for this before, but apparently demand wasn't high enough.
Maybe now is the time to ask again
Please, give us this day our switch, and deliver us from ElseIfs
Switch/Case statement
Re: Switch/Case statement
I'm presently studying C on my own. Interestingly, C seems to match AutoHotkey well. Where if you played with AutoHotkey, it's comfortable to look at C.
I think that maybe the issue with the Switch/Case statement, is it worked too similar to Loop/If. I'm not a pro or expert, but looks like they are pretty much doing the same thing. You run the Loop, and whenever a condition is met and true under the If statement, it is executed. You then "break" out of the Loop.
So, I don't understand what is the significant difference, where Switch/Case is needed. Also, I think AutoHotkey's Loop statement was a brilliant move. It is vastly easier for people that haven't formerly studied programming to figure out and use.
I think that maybe the issue with the Switch/Case statement, is it worked too similar to Loop/If. I'm not a pro or expert, but looks like they are pretty much doing the same thing. You run the Loop, and whenever a condition is met and true under the If statement, it is executed. You then "break" out of the Loop.
So, I don't understand what is the significant difference, where Switch/Case is needed. Also, I think AutoHotkey's Loop statement was a brilliant move. It is vastly easier for people that haven't formerly studied programming to figure out and use.
Re: Switch/Case statement
No the case Statement is not a loop.
Its similar to a lot of ifs combined.
Its similar to a lot of ifs combined.
Recommends AHK Studio
Re: Switch/Case statement
I meant that loop can do similar in my estimation, very similar. However, I'm definitely not an expert, so please explain what makes it significantly different so that it's adding something we can't do now.
Switch/Case
switch(expression) {
case constant-expression :
statement(s);
break
case constant-expression :
statement(s);
break
}
Loop,
{
If (expression)
{
statement(s)/command(s)
break
}
If (expression)
{
statement(s)/commands(s)
break
}
}
Switch/Case
switch(expression) {
case constant-expression :
statement(s);
break
case constant-expression :
statement(s);
break
}
Loop,
{
If (expression)
{
statement(s)/command(s)
break
}
If (expression)
{
statement(s)/commands(s)
break
}
}
Re: Switch/Case statement
A loop has nothing to do with a switch case statement.
Please read the description of the switch/case statement again.
Or for simplicity:
Please read the description of the switch/case statement again.
That would mean you could create a switch/case statement in AutoHotkey like this:The CASE statement evaluates a single expression and compares it against several potential values, or evaluates multiple Boolean expressions and chooses the first one that is TRUE
Code: Select all
if ( x = 1 )
...
else if ( x = 2 )
...
else if ( x = 3 )
...
else if ( x = 4 )
...
Code: Select all
switch := [ func( "xequals1" ), func( "xequals2" ), func( "xequals3" ), func( "xequals4" ) ]
switch[ x ].Call()
xequals4()
{
...
}
xequals3()
{
...
}
xequals2()
{
...
}
xequals1()
{
...
}
Recommends AHK Studio
Re: Switch/Case statement
YES, I want to pile on to this request. Adding a switch/case syntax is my #1+ wish for AHK. It comes up a lot in typical AHK applications: the user pressed key X, but what it does depends on some condition (e.g. window class) that has multiple cases.
Re: Switch/Case statement
You can get some good outcomes by using the ternary operator:
- Instead of a single assignment like b := "B", you could put multiple assignments within the round brackets, and/or you can use functions, e.g. function versions of commands.
- I would usually look up the value in an array, or use if/else if/else.
Code: Select all
q::
Loop, 5
{
a := A_Index
b := (a = 1) ? 10
: (a = 2) ? 20
: (a = 3) ? 30
: (a = 4) ? 40
: 0
MsgBox, % b
}
Loop, 5
{
a := A_Index
b := c := d := e := ""
(a = 1) ? (b := "B")
: (a = 2) ? (c := "C")
: (a = 3) ? (d := "D")
: (a = 4) ? (e := "E")
: 0
MsgBox, % b " " c " " d " " e
}
return
- I would usually look up the value in an array, or use if/else if/else.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: Switch/Case statement
Hey thanks! At first I didn't understand why your code works, I had to look back at the documentation to discover that an operator at the start of a line is parsed as a continuation of the previous line -- I never knew AHK did that! Learning something every day.
Re: Switch/Case statement
As Jeeswg says, one can always use another way instead of case/switch. However, case/switch is uniquely accessible and clear. The first time I ever saw case/switch in a bunch of code, I immediately understood more or less what it would do. It's easy to understand and to learn for laymen, which fits Autohotkey's philosophy. The same cannot be said for ternary operators! They're super efficient, granted, but also super hard to look at! Every time I see them, I recoil a little bit. So I think a case could be made for case/switch. I'd love to have it!
Re: Switch/Case statement
Not only laymen recoil every time they see a large row of ternary operators and switch could in fact be faster.
Recommends AHK Studio
Re: Switch/Case statement
Bumping this because my script is full of ugly walls of if/else and a switch/case implementation would be so much easier to read, not to mention more aesthetically pleasing.
Re: Switch/Case statement
I think lexicons at end in v1.1.29 has not implemented the switch statement, it's a pity
https://autohotkey.com/boards/viewtopic ... 82#p213870
https://autohotkey.com/boards/viewtopic ... 82#p213870
Code: Select all
F1::
WinGetTitle, text, MT9750
llarg := RegExReplace(text ,"(^.*\[)|(\].*$)") ; Retorna el text que hi ha entre []
curt := RegExReplace(llarg,"\d$") ; Es menja l'último digit
switch curt
{
case "NATTES":
case "NATEXP":
Msgbox 0x40000, % curt, % "NAT_NATEXPc()"
case "NATFAB":
case "NATFTE":
Msgbox 0x40000, % curt, % "NAT_NATFABc()"
case "PREDICT":
Msgbox 0x40000, % curt, % "BS2_PREDICT_1()"
default:
Msgbox % "NO SÉ QUÈ CONY VOLS OBRIR!!!" curt
}
Return
- Attachments
-
- TEST_SWITCH.png (5.24 KiB) Viewed 7786 times
Donec Perficiam
Re: Switch/Case statement
Code: Select all
default:
Msgbox % "NO SÉ QUÈ CONY VOLS OBRIR!!!" curt
Salutacions,
burque505
Re: Switch/Case statement
Coi un altre català per aquí
Suposo que en som més d'un
Donec Perficiam
Suposo que en som més d'un
Donec Perficiam
Donec Perficiam
Re: Switch/Case statement
Donec Perficiam!
Has llegit ' Catalunya durant la Guerra de Successió', oi?
Per cert, t'he enviat un PM.
Salutacions,
burque505
Has llegit ' Catalunya durant la Guerra de Successió', oi?
Per cert, t'he enviat un PM.
Salutacions,
burque505
Who is online
Users browsing this forum: No registered users and 27 guests