Switch/Case statement

Propose new features and changes
User avatar
svArtist
Posts: 62
Joined: 08 Mar 2015, 18:16

Switch/Case statement

05 Feb 2017, 14:50

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 :P
:morebeard:
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: Switch/Case statement

28 Feb 2017, 12:05

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.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Switch/Case statement

02 Mar 2017, 07:10

No the case Statement is not a loop.
Its similar to a lot of ifs combined.
Recommends AHK Studio
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: Switch/Case statement

06 Mar 2017, 02:11

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
}
}
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Switch/Case statement

06 Mar 2017, 03:21

A loop has nothing to do with a switch case statement.
Please read the description of the switch/case statement again.
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
That would mean you could create a switch/case statement in AutoHotkey like this:

Code: Select all

if ( x = 1 )
...
else if ( x = 2 )
...
else if ( x = 3 )
...
else if ( x = 4 )
...
Or for simplicity:

Code: Select all

switch := [ func( "xequals1" ), func( "xequals2" ), func( "xequals3" ), func( "xequals4" ) ]
switch[ x ].Call()
xequals4()
{
...
}

xequals3()
{
...
}

xequals2()
{
...
}

xequals1()
{
...
}
Recommends AHK Studio
Roel
Posts: 12
Joined: 27 Oct 2014, 16:40
Contact:

Re: Switch/Case statement

08 Dec 2017, 14:33

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.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Switch/Case statement

08 Dec 2017, 15:02

You can get some good outcomes by using the ternary operator:

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
- 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.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Roel
Posts: 12
Joined: 27 Oct 2014, 16:40
Contact:

Re: Switch/Case statement

08 Dec 2017, 15:19

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. :-)
User avatar
Cerberus
Posts: 172
Joined: 12 Jan 2016, 15:46

Re: Switch/Case statement

28 Jan 2018, 22:55

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!
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Switch/Case statement

29 Jan 2018, 00:52

Not only laymen recoil every time they see a large row of ternary operators and switch could in fact be faster.
Recommends AHK Studio
MaxAstro
Posts: 557
Joined: 05 Oct 2016, 13:00

Re: Switch/Case statement

18 May 2018, 09:17

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.
User avatar
jmeneses
Posts: 524
Joined: 28 Oct 2014, 11:09
Location: Catalan Republic

Re: Switch/Case statement

13 Jun 2018, 04:17

I think lexicons at end in v1.1.29 has not implemented the switch statement, it's a pity :roll:
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
TEST_SWITCH.png (5.24 KiB) Viewed 7567 times
Donec Perficiam
burque505
Posts: 1731
Joined: 22 Jan 2017, 19:37

Re: Switch/Case statement

14 Jun 2018, 09:54

:bravo:

Code: Select all

	  default:       
	  	    Msgbox % "NO SÉ QUÈ CONY VOLS OBRIR!!!" curt
Quin tip de riure!
Salutacions,
burque505
User avatar
jmeneses
Posts: 524
Joined: 28 Oct 2014, 11:09
Location: Catalan Republic

Re: Switch/Case statement

14 Jun 2018, 11:27

Coi un altre català per aquí :dance:
Suposo que en som més d'un
Donec Perficiam :bravo:
Donec Perficiam
burque505
Posts: 1731
Joined: 22 Jan 2017, 19:37

Re: Switch/Case statement

14 Jun 2018, 11:42

Donec Perficiam! :D
Has llegit ' Catalunya durant la Guerra de Successió', oi?
Per cert, t'he enviat un PM.
Salutacions,
burque505

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 21 guests