Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Case/Switch


  • Please log in to reply
44 replies to this topic
JSLover
  • Members
  • 920 posts
  • Last active: Nov 02 2012 09:54 PM
  • Joined: 20 Dec 2004

For the JSlover "solution", I think it's, as we say in our beautifull country...(a plaster on a wooden leg) (Sorry JSLover, absolutely no offense !!!)

...I didn't post it as a "solution"...as if I didn't want a real syntax...I want a real syntax (like C)...I just posted that cuz it works...

I can't see how...can be more readable than...

...between those 2, I like my workaround better...but C syntax is more like...

switch(msg){
	case WM_CLOSE:
		msgbox, no!
		break
	case WM_QUIT:
		msgbox, still, no!
		break
	case WM_PAINT:
		msgbox, you! paint yourself! I'm not doing it.
		break
	default:
		msgbox, give up eh?
		break
}
...C syntax don't cover anything but "equals"...but..."Case = Something"...is ambiguous...are you assigning the Case variable or using it in a Switch-Case?

Possible syntax?...

switch(msg){
	case in WM_CLOSE,WM_QUIT:       ;take matchlist as an expression?
		msgbox, no!
		break
	case in "WM_CLOSE,WM_QUIT":     ;make matchlist a string match?
		msgbox, no!
		break
	case in "WM_CLOSE","WM_QUIT":   ;or this way?
		msgbox, no!
		break
	case contains "Hi":             ;contains operator
		msgbox, Hi!
		break
	case [<operator-word>] "Hi":    ;<operator-word> is optional
		msgbox, Hi!
		break
	case "Hi":                      ;default operator is =
		msgbox, Hi!
		break
	case between 1,10:              ;less "wordy" between operator...
		msgbox, Hi!
		break
	case between anvil,hammer:      ;...with variables
		msgbox, Hi!
		break
	case <operator-word> "Hi":      ;expand with more <operator-word>s
		msgbox, Hi!
		break
}

...there is no real need for the colons...

...a case isn't a case without a colon (plus how else can it *hit?)...loooooool....

There is no break...

...there is no Switch-Case syntax yet, how can you say there is no break?...Chris has already said he likes break...or at least wants to do it that way...I like the cases being able to fall thru...you can Gosub if what you are doing requires a loop/break...or the inner break could just automatically break the loop & not the case.
Useful forum links: New content since: Last visitPast weekPast 2 weeks (links will show YOUR posts, not mine)

OMFG, the AutoHotkey forum is IP.board now (yuck!)...I may not be able to continue coming here (& I love AutoHotkey)...I liked phpBB, but not this...ugh...

Note...
I may not reply to any topics (specifically ones I was previously involved in), mostly cuz I can't find the ones I replied to, to continue helping, but also just cuz I can't stand the new forum...phpBB was soo perfect. This is 100% the opposite of "perfect".

I also semi-plan to start my own, phpBB-based AutoHotkey forum (or take over the old one, if he'll let me)
PM me if you're interested in a new phpBB-based forum (I need to know if anyone would use it)
How (or why) did they create the Neil Armstrong memorial site (neilarmstronginfo.com) BEFORE he died?

Demokos
  • Members
  • 105 posts
  • Last active: Jan 20 2011 06:28 PM
  • Joined: 28 Dec 2005
I'll try to be short !!!

@PhiLho : as you I think If ... Else If is actually sufficent and Switch|Select ... Case not necessary, but I think it can be usefull for a concise and more readable code. No more no less.
- Ok to have Case Droopy instead of Case = Droopy and to have Case not Droopy too...
- For the break I think it can be an option : use break if you want to skip the other tests, for example.

@JSLover : once again there wasn't anything in my mind to offense you and I hope it wasn't the case. But I think that between "Else If Case" and "Else If" I prefer the second one, and between "If ... Else If" and "Switch/Case" I prefer the second one. That's all... So if we have the Case syntax it will be a good thing, if not never mind we will continue to use If ... Else If without any problem.

for your syntax proposal, I am OK with it with some little changes :
switch(msg)
   {
   case "Hi", msgbox, Hi!          ;it is possible to have a one line Case
   case in WM_CLOSE,WM_QUIT        ;no need of double point       
      {                            ;If there are several instructions, we use brackets as usual in AHK
      Gosub, SomeLabel             ;if the condition is OK the code is executed
      msgbox, no!                  ;and the other tests are made (no break instruction)
      }
   case between anvil,hammer     
      {
      msgbox, no!                  ;If if the condition is OK the code is executed
      break                        ;with the break instruction all the others tests are skipped
      }                            ;of course if the condition is not OK, no break... 
   case contains "Hi"              ;And so on...
      {
      msgbox, no!
      break
      }
   default, msgbox, Ho!            ;Optional : if none of the condtions above are met [and without break instruction], do that...
   }
I don't know if it is in the Basic, or C, or any programming language spirit, but it is my proposal.
But again in my mind it would be nice but it is not urgent and not necessary.
We can use AHK without that and there are things more usefull to do before that.

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
Thanks for the syntax ideas; I've made a note to read this topic again when the time comes.

As some said, I tend to agree that this is a slightly lower priority than it used to be. However, I'm still leaning toward adding it at some point.

slotboxed
  • Members
  • 1 posts
  • Last active: Mar 10 2006 05:56 AM
  • Joined: 07 Mar 2006
It may be useful to read how php does it:
http://www.php.net/m...ures.switch.php

and javascript:
http://developer.moz...atements:switch


For breaks in nested control structures,

php uses the "break 2" style and javascript uses the "break label" style.

The "break label" isn't explained very well there. Here is a page that shows nested use and another type of use for the label:

http://www.devguru.c...kref/break.html


Here is an example of fall-through behavior (in php):
$filesizeStr = "4.37GB";
$sizevalue = (float) substr($filesizeStr, 0, -2);
$sizemult = substr($filesizeStr, -2, 1);
$bytes = 1;
switch($sizemult){
  case "G":
    $bytes *= 1024;
  case "M":
    $bytes *= 1024;
  case "K":
    $bytes *= 1024;
  default:
    $sizevalue *= $bytes;
    $bytesize = ceil($sizevalue);
}
// $bytesize = 4692251771


PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005

...a case isn't a case without a colon (plus how else can it *hit?)...loooooool....

Is it a pun? I don't get it...

There is no break...

...there is no Switch-Case syntax yet, how can you say there is no break?

My remark was on the syntax proposal of Demokos.
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
Thanks for the nice links and info. I do prefer to model AHK syntax after existing languages, especially when they have a particularly easy and useful syntax.

IvanZT
  • Members
  • 5 posts
  • Last active: Sep 13 2007 09:47 AM
  • Joined: 12 Jul 2007
As I can see now, this is still not implemented?

engunneer
  • Moderators
  • 9162 posts
  • Last active: Sep 12 2014 10:36 PM
  • Joined: 30 Aug 2005
since AHK 2.0 has not been released - that would appear to be true.

William
  • Guests
  • Last active:
  • Joined: --
I prefer 'others' to 'default'.

1. the 'others' is way existential and lowercase.*

2. Default could be interpreted as "always applied in addition to these cases"

3. Default may be misleading, because it can be made to handle a remote possibility.

-William



*Lowercase def.: everything that uppercase isn't.[/u]

argneo
  • Members
  • 220 posts
  • Last active: Mar 20 2015 09:18 PM
  • Joined: 14 Sep 2007
I was just looking for something like this to use in the multi-language auto-corrector... Will it be reviewed sometime soon?
Posted Image Search&Replace

Don't use cannon to kill mosquito

Dra_Gon
  • Members
  • 373 posts
  • Last active: Oct 27 2009 04:20 PM
  • Joined: 25 May 2007
Yo, is this one still on the board? Or is there a current workaround {i.e. function or such} which can be used?

I've used the "Select...Case...EndSelect" through IBDev and found it MUCH more useful than the "If...Else If". You'd think they would be about the same, but it just seems much clearer to me with the SCE. Just my 2 cents worth :wink: .

Ciao,
Dra'Gon
Posted Image
For a good laugh {hopefully} >> megamatts.50megs.com

My WritersCafe profile>>
http://www.writersca...BlueDragonFire/

  • Guests
  • Last active:
  • Joined: --

Yo, is this one still on the board?

...is this what still on board? My Switch-Case workaround? Yes...
Base.ahi
Switch-Case.ahi...I intended to move it into Base.ahi a long time ago, but haven't...

...& found it MUCH more useful than the "If...Else If".

...do you mean..."if (a=1) else if (a=2)"...or my..."switch(a) if case(1) else if case(2)"...my workaround uses if...else if, but only cuz it has too...

...but it just seems much clearer to me with the SCE.

...Select...EndSelect...or any of the Func...EndFunc...constructs annoy me...I like braces...{ } to mark the begin & end...

Dra_Gon
  • Members
  • 373 posts
  • Last active: Oct 27 2009 04:20 PM
  • Joined: 25 May 2007
Oh, by SCE I just meant that style. Using the brackets instead of EndSelect is just fine by me. And I simply prefer to use the Select..Case rather than the If...Else If myself. I understand that since AHK doesn't have it SOMEONE had to use the If...Else If kinda thing as a workaround. I'm just glad I didn't have to try to figure it out :wink: !

THANKS! I'll check 'em out.

Ciao,
Dra'Gon
Posted Image
For a good laugh {hopefully} >> megamatts.50megs.com

My WritersCafe profile>>
http://www.writersca...BlueDragonFire/

Dra_Gon
  • Members
  • 373 posts
  • Last active: Oct 27 2009 04:20 PM
  • Joined: 25 May 2007
Oh, now I see what you meant by yours using the If...Else If. Dang! Well, it does seem to clean it up a bit better. I'll use it and try to figure something as a function, just not sure how yet.

Ciao,
Dra'Gon
Posted Image
For a good laugh {hopefully} >> megamatts.50megs.com

My WritersCafe profile>>
http://www.writersca...BlueDragonFire/

Xystus777
  • Guests
  • Last active:
  • Joined: --
I'm a little confused on how to go about using these Switch-Case scenarios? I want to use a switch case in my code, how do I do this? Thanks.