AutoHotkey Community

It is currently May 27th, 2012, 1:27 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: Switch case and For
PostPosted: March 10th, 2009, 12:08 pm 
Offline

Joined: December 28th, 2005, 10:46 am
Posts: 99
I'd like to see two type of flow control in AHK :
a switch case
switch (expression)
{
case x:
bloc_1; break;
case y:
bloc_2; break;

. . . .

case const_n:
bloc_n; break;
default:
bloc_def
}

to make more readable a if ... else if suite of instructions

and a For lopp, to be able to do :

For x = 1, 5, 12, 1245
{
Instructions
}

or

For y = "abc", "zkg", "rstdg"
{
Instructions
}

or

For z = 144 step -12
{
Instructions
}

to be able to make loops on non contiguous values and with the possibility to uise steps...


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 10th, 2009, 1:04 pm 
Offline

Joined: June 18th, 2008, 8:36 am
Posts: 4923
Location: AHK Forum
Instead loop you can try While :)

For switch case you can do following:
AutoHotkey Help wrote:
Although a Goto whose target is outside the function is ignored, it is possible for a function to Gosub an external/public subroutine and then do a Goto from there.

So only internal goto labels will work :!:
Code:
Switch(1)
Switch(1+1)
Switch(3) ;will not be executed as outside of function
Return
Switch(case){
   If !IsLabel(case)
      Return
   Goto, %case%
   1:
   MsgBox Case 1
   Return
   2:
   MsgBox Case 2
   return
}
3:
MsgBox Case 3
return

_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun :wink:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 10th, 2009, 3:43 pm 
Offline

Joined: December 28th, 2005, 10:46 am
Posts: 99
Thanks HotKeyIt for your suggestions.
About your suggestion concerning the switch construction, I've seen before asking for this implementation the post [function] switch(case) demo, so I know thera are wrokarounds to have it, but I think that, even if it's just as effective as an internally coded function, it's look like DIY.
Not to say that a DIY solution is a bad one, but I think that an internally coded solution would made the AHK more readable.

For the For instruction, I think that when you speak about the 'While' instruction, you want to say that it ca be substitued to the 'For' one.

Also for readability (but not only), I'm not agree. If I took examples like
Quote:
For x = 1, 5, 12, 1245
or
Quote:
For y = "abc", "zkg", "rstdg"
, it's to indicate non progressives suites (1, 2, 3, 4, ...).

The example could have been : For x = 1, 1245, -4, 125, 32 to indicate that the suite of values to handle could be not progressive, but must be done in this order (1 in first, 1245 in second, ...).

Of course you can do that with a suite of If ... then If instructions, but I think it's more readable and efficient to be able to do it that way.

This improvements can be seen as cosmetic ones, but I think that the readability of the code is a good way to maintain it more efficiently.

(Sorry for the bad english...)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 10th, 2009, 5:59 pm 
Offline

Joined: June 18th, 2008, 8:36 am
Posts: 4923
Location: AHK Forum
As far as I understand both requests from you are more or less cosmetic suggestions?

If so I do not think it is going to happen, AHK is more for everyone and not for programmers only, so the less functions/commands the easier to understand and to learn AHK.

If not, and you have a problem to convert an for... into AHK try this:
Code:
for=1, 5, 12, 1245
StringSplit,for,for,`,
While (A_Index<=for0 and x:=for%A_Index%){
   MsgBox % x
}

_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun :wink:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 10th, 2009, 8:32 pm 
Offline

Joined: March 19th, 2006, 5:52 am
Posts: 419
You could also use Loop, Parse to do the for loop like you requested.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 11th, 2009, 5:47 am 
Offline

Joined: December 28th, 2005, 10:46 am
Posts: 99
HotKeyIt wrote:
As far as I understand both requests from you are more or less cosmetic suggestions?

Yes indeed, they are not a necessity to make an AHK script. I know they are workaroud for them. But I think that it is the case of the of the new while ... loop construction, too, that can be replaced by an other ahk type of loop.
But, as the while ... loop, they are usefull to make the code more readable, and so more easily maintainable.
Of course, I don't consider that they must be in the higher place on the priority list, but I think it would be a good thing to have them, if it's possible.
No more nor less.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 17th, 2009, 5:02 pm 
an autohotkey soultion for switch/case was previously done.

http://www.autohotkey.com/forum/topic7350.html&highlight=switch+pswitch+returnlast


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 25th, 2009, 10:15 pm 
Yes, Switch case, I know that there are workarounds for these instructions (switch ... case and for), and I saw the one you point to.
As for the new while loop, they are not necessary to make a script in AHK (an If ... Then ... Else structure can be used to replace these instructions).
But, again, I think they would be a good thing to make the code more easily readable, so more easy to maintain.
Of course I think it isn't a high priority.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 29th, 2009, 6:50 am 
Offline

Joined: November 4th, 2008, 9:23 am
Posts: 1045
If you want a for loop: for start to end, step, check out [Func] for and forE - emulate a for loop.

_________________
As always, if you have any further questions, don't hesitate to ask.

Add OOP to your scripts via the Class Library. Check out my scripts.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 29th, 2009, 1:30 pm 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
One word on fake constructs. Most fake constructs are not making really easier to read. They are slower as usual and the programmer must take care about that construct, where a built-in construct would be checked for correctness by AutoHotkey.

Switch-Case is not common, but may be like while good to have. As most other programming languages have that. The For constructs like in C or Pascal are also often requested.

But look, if every asked constructed would be implemented, then the language would grow too much. This leads to more possible bugs and more to learn and confuse for beginners. Try to explain non programmers the C type of For...


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 2 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group