Page 1 of 1

Switch/Case statement

Posted: 05 Feb 2017, 14:50
by svArtist
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

Re: Switch/Case statement

Posted: 28 Feb 2017, 12:05
by SOTE
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.

Re: Switch/Case statement

Posted: 02 Mar 2017, 07:10
by nnnik
No the case Statement is not a loop.
Its similar to a lot of ifs combined.

Re: Switch/Case statement

Posted: 06 Mar 2017, 02:11
by SOTE
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
}
}

Re: Switch/Case statement

Posted: 06 Mar 2017, 03:21
by nnnik
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()
{
...
}

Re: Switch/Case statement

Posted: 08 Dec 2017, 14:33
by Roel
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

Posted: 08 Dec 2017, 15:02
by jeeswg
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.

Re: Switch/Case statement

Posted: 08 Dec 2017, 15:19
by Roel
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

Posted: 28 Jan 2018, 22:55
by Cerberus
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

Posted: 29 Jan 2018, 00:52
by nnnik
Not only laymen recoil every time they see a large row of ternary operators and switch could in fact be faster.

Re: Switch/Case statement

Posted: 18 May 2018, 09:17
by MaxAstro
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

Posted: 13 Jun 2018, 04:17
by jmeneses
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


Re: Switch/Case statement

Posted: 14 Jun 2018, 09:54
by burque505
:bravo:

Code: Select all

	  default:       
	  	    Msgbox % "NO SÉ QUÈ CONY VOLS OBRIR!!!" curt
Quin tip de riure!
Salutacions,
burque505

Re: Switch/Case statement

Posted: 14 Jun 2018, 11:27
by jmeneses
Coi un altre català per aquí :dance:
Suposo que en som més d'un
Donec Perficiam :bravo:

Re: Switch/Case statement

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