Topic: conditions in if stmt
Author: Bret Pehrson <bretp@strata3d.com>
Date: 1996/04/30 Raw View
Could someone please answer this with the correct answer:
Is the order of evaluation of the conditions in an if (or any other
conditional for that matter) specified in either the C or C++ standard?
If so, what does it say.
The reason being is that I would like leftmost conditions to prevalidate
rightmost conditions without having to have multiple conditionals.
For example, rather than:
char * pszBuffer = ...
if (pszBuffer != NULL)
{
if (pszBuffer[0] == 'a')
...
I would prefer:
if ((pszBuffer != NULL) && (pszBuffer[0] == 'a'))
...
Tnx
--
Bret Pehrson BretP@strata3d.com
*Please respond to newsgroup unless specified otherwise
--
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: austern (Matt Austern)
Date: 1996/04/30 Raw View
In article <31866476.4691@strata3d.com> Bret Pehrson <bretp@strata3d.com> writes:
> The reason being is that I would like leftmost conditions to prevalidate
> rightmost conditions without having to have multiple conditionals.
>
> For example, rather than:
> char * pszBuffer = ...
> if (pszBuffer != NULL)
> {
> if (pszBuffer[0] == 'a')
> ...
>
> I would prefer:
> if ((pszBuffer != NULL) && (pszBuffer[0] == 'a'))
It's guaranteed that this will work. It has nothing to do with it
being an if statement, though: what's relevant is the behavior of the
&& and || operators. The draft C++ standard guarantees (see section
5.14 [expr.log.and] and 5.15 [expr.log.or]) that the second operand of
&& will not be evaluated if the first is false, and that the second
operand of || will not be evaluated if the first is true.
Note that this is a special case and that it applies only to the
built-in operators && and ||. If you overload them yourself, for some
user-defined types, then both operands are evaluated just as with any
other operators or functions.
--
Matt Austern
SGI: MTI Compilers Group
austern@isolde.mti.sgi.com
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Michael R Cook <mcook@cognex.com>
Date: 1996/05/01 Raw View
>>>>> "BP" == Bret Pehrson <bretp@strata3d.com> writes:
BP> Could someone please answer this
Sure!
BP> with the correct answer:
Oh...correct...so demanding! :-)
BP> if ((pszBuffer != NULL) && (pszBuffer[0] == 'a'))
That does what you want. `&&' and `||' have always worked that way,
since K&R I.
5.14 Logical AND operator [expr.log.and]
1 logical-and-expression:
inclusive-or-expression
logical-and-expression && inclusive-or-expression
The && operator groups left-to-right. The operands are both implic-
itly converted to type bool (_conv_). The result is true if both
operands are true and false otherwise. Unlike &, && guarantees left-
to-right evaluation: the second operand is not evaluated if the first
operand is false.
Michael.
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]