AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Case/Switch + If xxx And xxx

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
hypertyper



Joined: 12 Nov 2009
Posts: 17

PostPosted: Sun Feb 21, 2010 4:05 pm    Post subject: Case/Switch + If xxx And xxx Reply with quote

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
View user's profile Send private message
tidbit



Joined: 09 Mar 2008
Posts: 1807
Location: Minnesota, USA

PostPosted: Sun Feb 21, 2010 4:22 pm    Post subject: Reply with quote

you know that you are aloud to try code BEFORE asking if it will work Wink.
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
View user's profile Send private message
answer4u
Guest





PostPosted: Sun Feb 21, 2010 4:23 pm    Post subject: Reply with quote

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

PostPosted: Sun Feb 21, 2010 4:40 pm    Post subject: Reply with quote

tidbit wrote:
you know that you are aloud to try code BEFORE asking if it will work Wink.
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
View user's profile Send private message
answer4u
Guest





PostPosted: Sun Feb 21, 2010 4:42 pm    Post subject: Reply with quote

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

PostPosted: Sun Feb 21, 2010 4:45 pm    Post subject: Reply with quote

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
View user's profile Send private message
hypertyper



Joined: 12 Nov 2009
Posts: 17

PostPosted: Sun Feb 21, 2010 4:53 pm    Post subject: Reply with quote

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 Laughing

so this works:

Code:
MButton UP::
colour = red
players = 2

if (colour = "red")

msgbox, Red

Else
msgbox, Not red

Return
Back to top
View user's profile Send private message
answer4u
Guest





PostPosted: Sun Feb 21, 2010 4:56 pm    Post subject: Reply with quote

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

PostPosted: Sun Feb 21, 2010 5:02 pm    Post subject: Reply with quote

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 Smile
_________________
paradigm.shift:=(•_•)┌П┐RTFM||^.*∞
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group