Switch/Case statement

Propose new features and changes
User avatar
svArtist
Posts: 32
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
SOTE
Posts: 75
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: 2506
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: 75
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: 2506
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] [Download] GeSHi © Codebox Plus

if ( x = 1 )
...
else if ( x = 2 )
...
else if ( x = 3 )
...
else if ( x = 4 )
...

Or for simplicity:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

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: 3591
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] [Expand] [Download] GeSHi © Codebox Plus


- 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.
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: 63
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: 2506
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

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 6 guests