! or 'not' to validate an expression in an if statement as false?

Post a reply


In an effort to prevent automatic submissions, we require that you complete the following challenge.
Smilies
:D :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :| :mrgreen: :geek: :ugeek: :arrow: :angel: :clap: :crazy: :eh: :lolno: :problem: :shh: :shifty: :sick: :silent: :think: :thumbup: :thumbdown: :salute: :wave: :wtf: :yawn: :facepalm: :bravo: :dance: :beard: :morebeard: :xmas: :HeHe: :trollface: :cookie: :rainbow: :monkeysee: :monkeysay: :happybday: :headwall: :offtopic: :superhappy: :terms: :beer:
View more smilies

BBCode is ON
[img] is OFF
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: ! or 'not' to validate an expression in an if statement as false?

Re: ! or 'not' to validate an expression in an if statement as false?

Post by john_c » 11 Sep 2019, 15:08

nnnik wrote:
11 Apr 2018, 07:14
depending on who you ask there might be several answers
Related: https://stackoverflow.com/questions/45701586/comparing-a-variable-to-multiple-strings-in-autohotkey

Re: ! or 'not' to validate an expression in an if statement as false?

Post by Cerberus » 03 Jun 2018, 20:44

tidbit wrote:also, the "if var in/contains/type" style if's are on a separate page than normal if's for a reason: they are different than normal if's.
they are not expressions, cannot use (), cannot use with || && and so on. they need to be EXACTLY as documented on their own page, not as documented on some other page (normal if's).

(same goes for hotkeys and send. so many people try to do {enter}:: or w/e. why? SEND docs are a completed different page than hotkey docs. stick with the correct page, folks! You wouldn't go read a monster trucks manual to fix a moped.)
Don't you think perhaps a warning would be in order on those pages, so that people won't mix together two incompatible modes? I believe "if"* does contain some nice warnings about the brackets and expression mode, but I don't know about the
"contains" page or section (or wherever that term is described).

Re: ! or 'not' to validate an expression in an if statement as false?

Post by tidbit » 11 Apr 2018, 10:56

also, the "if var in/contains/type" style if's are on a separate page than normal if's for a reason: they are different than normal if's.
they are not expressions, cannot use (), cannot use with || && and so on. they need to be EXACTLY as documented on their own page, not as documented on some other page (normal if's).

(same goes for hotkeys and send. so many people try to do {enter}:: or w/e. why? SEND docs are a completed different page than hotkey docs. stick with the correct page, folks! You wouldn't go read a monster trucks manual to fix a moped.)

Re: ! or 'not' to validate an expression in an if statement as false?

Post by john_c » 11 Apr 2018, 07:23

@nnnik Thanks. Now there is another good option.

Re: ! or 'not' to validate an expression in an if statement as false?

Post by nnnik » 11 Apr 2018, 07:14

Yeah they are not expressions and are inconsistent with expression syntax - due to that they are not an option.
Sadly there is not a simple alternative available - depending on who you ask there might be several answers:

Code: Select all

isDay := { moday:1, tuesday:1, wednesday:1, thursday:1, friday:1, saturday:1, sunday:1 }
if ( isDay[ day ] )
	Msgbox % day " is the name for a day"
Depending onthe circumstances different answers might be better

Re: ! or 'not' to validate an expression in an if statement as false?

Post by john_c » 11 Apr 2018, 07:04

@nnnik
nnnik wrote:Yeah you are on to something however you got it backwards.
NEVER USE CONTAINS.
Thanks. But there are some questions from my side:

1. Why we should avoid it?

2. Do you suggest to use inStr() or regExMatch() instead?

Code: Select all

var := "foo"
if inStr(var, "foo") || inStr(var, "bar")
    msgBox % "Success"

var := "foo"
if regExMatch(var, "(foo|bar)")
    msgBox % "Success"
3. Also, do you suggest to avoid if var in and if var between as well?

Re: ! or 'not' to validate an expression in an if statement as false?

Post by nnnik » 11 Apr 2018, 06:51

Yeah you are on to something however you got it backwards.
NEVER USE CONTAINS.

Re: ! or 'not' to validate an expression in an if statement as false?

Post by john_c » 11 Apr 2018, 06:45

tidbit wrote:Always. Stay consistent, avoid any issues (it's a common issue for a noob to not use () and simply adding them solves the issue).
From what I tested, () isn't always a good idea. Here it is:

Code: Select all

; Works correctly
var := "aaa"
if var contains foo,bar,baz
    msgBox % "Success"
    
; As I understand, parentheses are illegal here
var := "aaa"
if (var contains foo,bar,baz)
    msgBox % "Success"

Re: ! or 'not' to validate an expression in an if statement as false?

Post by tidbit » 19 Apr 2016, 12:10

like my example on the previous page. if (EVERYTHING)
if (var)
if (var && var2 || func())
if ((var3 || var) && var2)

not a bunch of ()'s like some people prefer: if (var) || (var2+5) || (func()-1)

Re: ! or 'not' to validate an expression in an if statement as false?

Post by Leli196 » 19 Apr 2016, 11:52

Interesting. How about an expression with several parentheses like this: (A_Index > 1) || (A_Index = 1 && !current_ip != ""). Would you put this as a whole in parentheses as well when used with an if statement? So if ((A_Index > 1) || (A_Index = 1 && !current_ip != ""))

Greetings Leli196

Re: ! or 'not' to validate an expression in an if statement as false?

Post by tidbit » 19 Apr 2016, 10:49

always. stay consistent, avoid any issues (it's a common issue for a noob to not use () and simply adding them solves the issue), save work later on if you need to add other stuff with && or || or if you prefer the { on the same line as the if/while/etc (OTB style)
if (GetKeyState("a", "P"))

though, with some functions like getkeystate, fileexist, and others, I do not always include an "=1" or "=0" (as I mentioned earlier that I always add them). the names make it obvous when reading.
"if the file exists..." is perfectly readable.
"if the file exists=1..." just looks silly :P

Re: ! or 'not' to validate an expression in an if statement as false?

Post by Leli196 » 19 Apr 2016, 10:19

tidbit wrote:I also always put ()'s on my if's, for consistency. if's without ()'s can behave funky. just always use ()'s to be safe, IMO
Although this post is not new: Do you even write if (GetKeyState("a", "P")) and not if GetKeyState("a", "P")?
How about the other people here? Do you use parantheses around functions?

Greetings Leli196

Re: ! or 'not' to validate an expression in an if statement as false?

Post by just me » 19 Apr 2016, 01:40

SifJar wrote:But there's no "or" symbol in there :P To me (and clearly others), it simply reads as "less than greater than" ...
If you look at <= there's no "or" symbol in there, too. So you read it as "less than equal", which would essentially be equivalent to < (less than)?

If (A <> B) results to false, A actually is either less or greater than B, and that's exactly what "not equal" stands for. And it's true for number as well as string comparisons.

Re: ! or 'not' to validate an expression in an if statement as false?

Post by SifJar » 18 Apr 2016, 17:46

Leli196 wrote:Well, it probably means something like less than or greater than , that makes sense to me. However, this may be somehow reasonable when used with numbers; but when it comes to strings less than or greater than is rather stupid.
But there's no "or" symbol in there :P To me (and clearly others), it simply reads as "less than greater than", which would essentially be equivalent to <= (less than or equal to), as "greater than" includes all numbers above the number in question, so if we're looking for anything LESS than that, it would be that number or anything less. Considering integers, for example,

consider x <> 5

breaking that down, >5 means 6, 7, 8, 9 etc.
therefore <(>5) would be < 6, 7, 8, 9 etc.,
which simplifies to < 6
i.e. 5, 4, 3, etc.
i.e. <= 5

And yes, when it comes to non-numeric variables, it makes zero sense at all.

In short, I agree with those who said this symbol doesn't make sense :P and naturally I prefer !=, which I was familiar with from other languages when I started AHK, and seems more intuitive.

Re: ! or 'not' to validate an expression in an if statement as false?

Post by Leli196 » 18 Apr 2016, 16:45

tidbit wrote:I'll move this topic to off-topic. it's not really asking for help, just formatting/style prefs.
True. I didn't know where to put something like this, now I know better for the future.
tidbit wrote:I hate <>.
! means "not" = means "equals". not equals. makes sense. "var not equals 5"
< means "less than" > means "greater than". less than greater than. makes no sense. "var less than greater than 5"
Well, it probably means something like less than or greater than , that makes sense to me. However, this may be somehow reasonable when used with numbers; but when it comes to strings less than or greater than is rather stupid.

Greetings Leli196

Re: ! or 'not' to validate an expression in an if statement as false?

Post by tidbit » 18 Apr 2016, 16:34

I hate <>.
! means "not" = means "equals". not equals. makes sense. "var not equals 5"
< means "less than" > means "greater than". less than greater than. makes no sense. "var less than greater than 5"

I'll move this topic to off-topic. it's not really asking for help, just formatting/style prefs.

Re: ! or 'not' to validate an expression in an if statement as false?

Post by Exaskryz » 18 Apr 2016, 16:26

I guess I hadn't chimed in with my use of !. I like the ! for the reason tidbit gave with it being visually outstanding from commands and variables. That applies to || and && too.

I use != because that reads left to right to me as "not equals", which is what that means. The <> just isn't intuitive to me.

Re: ! or 'not' to validate an expression in an if statement as false?

Post by wolf_II » 18 Apr 2016, 15:55

I use And, Or or Not, when I see a chance, that I might still be able to understand what I just coded in about 2 months time.
(Increased readability, you know, self-explaining code.)

I use &&, || or ! along with comments, if I can see that my code is too complex for that.

I prefer != over <> because it looks closer to that I know from school.

Re: ! or 'not' to validate an expression in an if statement as false?

Post by Leli196 » 18 Apr 2016, 15:28

tidbit wrote:I always use !. symbols stand out more than words. using "not" "and" "or" in if's and friends might get confusing since they might look like variables/text. a symbol breaks stuff up nicely.

Code: Select all

if (var and var2)
if (var && var2)
if (not var or var2=3)
if (!var || var2=3)
plus, "!=" exists, there's no "not=". so it helps with consistency.

I also always put ()'s on my if's, for consistency. if's without ()'s can behave funky. just always use ()'s to be safe, IMO
How about the other people here? Do you use and/or or &&/||? Until now I have been always using the former and did not question it. However tidbit's point seems very reasonable and I could think of changing my behavior and use the latter.

Furthermore it would be interesting to know whether you prefer != over <> or the other way around.

Greetings Leli196

Re: ! or 'not' to validate an expression in an if statement as false?

Post by Shadowpheonix » 18 Apr 2016, 14:56

Exaskryz wrote:
Shadowpheonix wrote: My problem is that when looking at the more complex if statements like if (!var || var2=3), I have a tendency to read it as lvar instead of !var.

I absolutely agree about always using ()'s though. :D
I think that may be a problem with your font though. ! vs l is pretty clear to me in my Notepad++. I'm using courier new: !lIi1
I think it is more a matter of me trying to speed-read when I am not very good at doing so. No issues if I actually slow down and deliberately read every word. :lol:

Top