By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,193 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

Why not to recommend leaving off <>0 in conditionals

P: n/a
I was just working on fixing another programmer's code, and he has apparently
been told about the fact that you can omit comparisons with zero for If Then
statements because zero is false, and non-zero is True. The trouble is, he
generalized that statement to mean things it doesn't and has some very bad
logic as a result.

It was something like this...

If InStr(str1, "ABC") And Not InStr(str1, "DEF") Then

Of course, "And" is going to do a binary comparison here, so it will generally
fail to work properly when "DEF" is in the string. In most cases, the branch
-will- execute when "DEF" is in there.
Nov 12 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a


Well after the problem with equating 0 ==> False and Non-0 ==> True, it's
not so much the AND as the NOT that is the problem.

<g>

--
Terry Kreft
MVP Microsoft Access
"Steve Jorgensen" <no****@nospam.nospam> wrote in message
news:9a********************************@4ax.com...
I was just working on fixing another programmer's code, and he has apparently been told about the fact that you can omit comparisons with zero for If Then statements because zero is false, and non-zero is True. The trouble is, he generalized that statement to mean things it doesn't and has some very bad
logic as a result.

It was something like this...

If InStr(str1, "ABC") And Not InStr(str1, "DEF") Then

Of course, "And" is going to do a binary comparison here, so it will generally fail to work properly when "DEF" is in the string. In most cases, the branch -will- execute when "DEF" is in there.

Nov 12 '05 #2

P: n/a
Yes, but it's the "And" that makes it pass the initial tests, so the problem
likely won't be discovered until run-time.

On Fri, 23 Apr 2004 12:25:52 +0100, "Terry Kreft" <te*********@mps.co.uk>
wrote:


Well after the problem with equating 0 ==> False and Non-0 ==> True, it's
not so much the AND as the NOT that is the problem.

<g>


Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.