examples of swtich statements in ahk2
- User
examples of swtich statements in ahk2
- Assistant
In AutoHotkey v2, the
switchstatement is used to execute different blocks of code based on the value of an expression. Here are some examples demonstrating how to useswitchstatements in AHK v2.Example 1: Basic Switch Statement
value := 2 switch value { case 1: MsgBox "The value is 1." case 2: MsgBox "The value is 2." case 3: MsgBox "The value is 3." default: MsgBox "The value is something else." }Example 2: Using Switch with Strings
color := "blue" switch color { case "red": MsgBox "The color is red." case "green": MsgBox "The color is green." case "blue": MsgBox "The color is blue." default: MsgBox "Unknown color." }Example 3: Using Switch to Handle Multiple Cases
grade := "B" switch grade { case "A": case "A+": MsgBox "Excellent!" case "B": MsgBox "Good job!" case "C": MsgBox "You passed." default: MsgBox "Needs improvement." }Example 4: Switch with Expressions
num := 7 switch num % 2 { case 0: MsgBox "The number is even." case 1: MsgBox "The number is odd." }Example 5: Using Switch with Functions
GetDayOfWeek() { return A_Now >>> 5 } switch GetDayOfWeek() { case 1: MsgBox "It's Sunday." case 2: MsgBox "It's Monday." case 3: MsgBox "It's Tuesday." case 4: MsgBox "It's Wednesday." case 5: MsgBox "It's Thursday." case 6: MsgBox "It's Friday." case 7: MsgBox "It's Saturday." }Explanation of the Examples
-
Basic Switch Statement: The program checks the value of a variable and shows a message box with a corresponding message.
-
Switch with Strings: This illustrates how to use non-numeric values with a
switchstatement. -
Multiple Cases: Multiple
casestatements can be combined to execute the same block of code for different values. -
Expressions: The switch can evaluate expressions to determine which case matches.
-
Function Example: A switch statement determines the day of the week based on a function return value.
These examples should give you a solid foundation for using switch statements in AutoHotkey v2. Adjust the code according to your needs for different scenarios.
-