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

How predicates are evaluated

P: n/a
If I have something like:

if (dx<0 and dy>=SIZE and m[dx][dy]!=0){
...
}

Can I assume that m[dx][dy]!=0 would be testes last or to be sure I
need to nest it?

if (dx<0 and dy>=SIZE){
if (m[dx][dy]!=0){
...
}
}

Mar 8 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Gaijinco wrote:
if (dx<0 and dy>=SIZE and m[dx][dy]!=0){
...
}
The built-in short-circuit operators ('||' aka 'or' and '&&' aka 'and')
are evaluated from left to right and stop evaluating as soon as the
result of the expression is determined. The same does *not* apply to
user defined operators whose operands are evaluated in some order.
Can I assume that m[dx][dy]!=0 would be testes last or to be sure I
need to nest it?


It depends on what the result type of the other involved operations
is and whether there is an overload for one of the logical operators
involving these types. In usual contexts you can normally ignore the
possibility of overloaded operators but in template code you should
play safe, i.e. make sure that the built-in operators are used, e.g.:

if (bool(dx < 0) and bool(dy >= SIZE) and bool(m[dx][dy] != 0))
...

Of course, the above expression assumes that 'm' is a suitably offset
pointer such that a negative index is allowed at all.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Mar 8 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.