| View previous topic :: View next topic |
| Author |
Message |
hypertyper
Joined: 12 Nov 2009 Posts: 17
|
Posted: Sun Feb 21, 2010 4:05 pm Post subject: Case/Switch + If xxx And xxx |
|
|
back with another basic question that google couldn't help me with. Is there anything like a switch/case syntax (?) in AHK?
I think there was in AutoIt... I currently use a load of "If"s but it's pretty ugly.
Second question: What do I do if I need two conditions to apply in one "if"? I need something like:
if (a > 2 and b < 5)
xxx
Thanks, again. |
|
| Back to top |
|
 |
tidbit
Joined: 09 Mar 2008 Posts: 1807 Location: Minnesota, USA
|
Posted: Sun Feb 21, 2010 4:22 pm Post subject: |
|
|
you know that you are aloud to try code BEFORE asking if it will work .
| Code: | a=6
b=4
if (a > 2 and b < 5) ; you may use &, &&, and, |, || , or
{
msgbox
run, notepad
} |
and no, no case-break in AHK. _________________ rawr. be very afraid
*poke*
Note: My name is all lowercase for a reason.
Even monkeys fall from trees. - Japanese proverb
Last edited by tidbit on Sun Feb 21, 2010 4:24 pm; edited 1 time in total |
|
| Back to top |
|
 |
answer4u Guest
|
Posted: Sun Feb 21, 2010 4:23 pm Post subject: |
|
|
| Quote: | | Is there anything like a switch/case syntax (?) in AHK? |
Not yet, but you could use the ternary operator: | Code: | x := 10
test := x=1 ? "one"
: x=2 ? "two"
: x=3 ? "three"
: x=4 ? "four"
: "other"
MsgBox, %test% |
| Quote: | | What do I do if I need two conditions to apply in one "if"? |
Your syntax is correct - you could add brackets if you want multiple lines:
| Code: | a := 3, b := 3
if (a > 2 and b < 5) {
MsgBox, 1
MsgBox, 2
} |
|
|
| Back to top |
|
 |
hypertyper
Joined: 12 Nov 2009 Posts: 17
|
Posted: Sun Feb 21, 2010 4:40 pm Post subject: |
|
|
| tidbit wrote: | you know that you are aloud to try code BEFORE asking if it will work .
| Code: | a=6
b=4
if (a > 2 and b < 5) ; you may use &, &&, and, |, || , or
{
msgbox
run, notepad
} |
and no, no case-break in AHK. |
I DID try that. I also looked at some existing scripts but couldn't find anything. Why is my script not working?
I had tried if (a > 3 and b = blue)
so I used a string rather than a number. Is that not allowed? I tried putting it in parenthesis but that doesn't seem to work either.
Thank you for your patience, I promise I am trying but I'm doing everything wrong I possibly could...
| Code: | if (Color = "Blue" or Color = "White")
{
MsgBox The color is one of the allowed values.
ExitApp
} |
This exact thing is in the documentation so what am I doing wrong?
this is what I'm playing around with:
| Code: | MButton UP::
colour = "red"
players = 2
if (colour = "red")
msgbox, Red
Else
msgbox, Not red
Return |
Last edited by hypertyper on Sun Feb 21, 2010 4:48 pm; edited 2 times in total |
|
| Back to top |
|
 |
answer4u Guest
|
Posted: Sun Feb 21, 2010 4:42 pm Post subject: |
|
|
| Code: | I had tried if (a > 3 and b = "blue")
so I used a string rather than a number. |
|
|
| Back to top |
|
 |
tidbit
Joined: 09 Mar 2008 Posts: 1807 Location: Minnesota, USA
|
Posted: Sun Feb 21, 2010 4:45 pm Post subject: |
|
|
if ()
...
the () make it an expression.
strings inside expressions need to be quoted.
| Code: | if (blah == "blue")
if (blah = "blue")
if (car=12 && blah == "blue")
if (car==12 & blah = "blue") |
_________________ rawr. be very afraid
*poke*
Note: My name is all lowercase for a reason.
Even monkeys fall from trees. - Japanese proverb |
|
| Back to top |
|
 |
hypertyper
Joined: 12 Nov 2009 Posts: 17
|
Posted: Sun Feb 21, 2010 4:53 pm Post subject: |
|
|
| Code: | MButton UP::
colour = "red"
players = 2
if (colour = "red")
msgbox, Red
Else
msgbox, Not red
Return |
It's telling me it's not red. I'm going crazy.
lol
so when you define it you need to leave the parenthesis out and in the if bracket you need to have it in. You're not making my life easy here
so this works:
| Code: | MButton UP::
colour = red
players = 2
if (colour = "red")
msgbox, Red
Else
msgbox, Not red
Return |
|
|
| Back to top |
|
 |
answer4u Guest
|
Posted: Sun Feb 21, 2010 4:56 pm Post subject: |
|
|
| Code: | colour := "red" ; or colour = red
players = 2
if (colour = "red") ; no blank line
msgbox, Red
Else
msgbox, Not red
Return |
|
|
| Back to top |
|
 |
TLM
Joined: 21 Aug 2006 Posts: 2926 Location: The Shell
|
Posted: Sun Feb 21, 2010 5:02 pm Post subject: |
|
|
Or this would have worked | Code: | MButton UP::
colour := "red"
players = 2
if (colour = "red")
msgbox, Red
Else
msgbox, Not red
Return |
edit
answer4u beat me to it  _________________
paradigm.shift:=(•_•)┌П┐RTFM||^.*∞ |
|
| Back to top |
|
 |
|