combine more than one switch case? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
xMaxrayx
Posts: 352
Joined: 06 Dec 2022, 02:56
Contact:

combine more than one switch case?

27 Apr 2024, 14:15

Hi, in if statment you can add logical || and && is a possible to do something similar with switch statement? I don't want re-write the same code or write outside function

Code: Select all

 case 1 && 2 :
 	msgbox
 
and if not can I make case 3 do the same thing with case 4 without copy-paste codde or writing a function?

Code: Select all

case 3: 
	Msgbox
case 4:
	do case 3

-----------------------ヾ(•ω•`)o------------------------------
https://github.com/xmaxrayx/
User avatar
mikeyww
Posts: 28871
Joined: 09 Sep 2014, 18:38

Re: combine more than one switch case?

27 Apr 2024, 14:39

Each case may list up to 20 values. Each value must be an expression, but can be a simple one such as a literal number, quoted string or variable.
Source: Switch - Syntax & Usage | AutoHotkey v2
Any expression is fine.

I'm puzzled about why you want to ask this question instead of testing your idea in your script. The test requires about 45 seconds.

Code: Select all

#Requires AutoHotkey v2.0
Switch {
 Case 1 && 2:
  MsgBox
}
User avatar
Seven0528
Posts: 497
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: combine more than one switch case?  Topic is solved

27 Apr 2024, 22:25

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force

var := "0x0003"
;  A.
switch (var)
{
    default:
    case 1:
    case 2:
    case 3,4:		msgbox var
}
;  B.
switch
{
    default:
    case (var==1):
    case (var==2):
    case (var==3||var==4):      msgbox var
}
;  C.
switch (var)
{
    case 1:
    case 2:
    default:
        switch
        {
            case (format("{:d}",var)~="D)^(3|4)$"):      msgbox var               
            default:
        }
}
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
User avatar
xMaxrayx
Posts: 352
Joined: 06 Dec 2022, 02:56
Contact:

Re: combine more than one switch case?

28 Apr 2024, 11:38

mikeyww wrote:
27 Apr 2024, 14:39
Each case may list up to 20 values. Each value must be an expression, but can be a simple one such as a literal number, quoted string or variable.
Source: Switch - Syntax & Usage | AutoHotkey v2
Any expression is fine.

I'm puzzled about why you want to ask this question instead of testing your idea in your script. The test requires about 45 seconds.

Code: Select all

#Requires AutoHotkey v2.0
Switch {
 Case 1 && 2:
  MsgBox
}
sorry I didn't know you could use && , || expressions in switch cases because most examples are with IF statements,also I'm not that good.

a lot of old c# totrial they use
case1::
case 2::
case 3::
msgbox
break

so I thought there was something similar to AHK
-----------------------ヾ(•ω•`)o------------------------------
https://github.com/xmaxrayx/
User avatar
xMaxrayx
Posts: 352
Joined: 06 Dec 2022, 02:56
Contact:

Re: combine more than one switch case?

28 Apr 2024, 11:41

Seven0528 wrote:
27 Apr 2024, 22:25

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force

var := "0x0003"
;  A.
switch (var)
{
    default:
    case 1:
    case 2:
    case 3,4:		msgbox var
}
;  B.
switch
{
    default:
    case (var==1):
    case (var==2):
    case (var==3||var==4):      msgbox var
}
;  C.
switch (var)
{
    case 1:
    case 2:
    default:
        switch
        {
            case (format("{:d}",var)~="D)^(3|4)$"):      msgbox var               
            default:
        }
}

I see many thanks so switch cases have a lot of power like with IF statement.
-----------------------ヾ(•ω•`)o------------------------------
https://github.com/xmaxrayx/

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: DavidP, FanaticGuru and 103 guests