Using the && operator as a short-curcuit

Started by
16 comments, last by Sean99 21 years, 7 months ago
It just takes a full understanding of how those operators are handled really.

This is the way I remember it, and it''s quite a nice way of remembering it as well. With Logical OR, statements are executed until TRUE is found. With Logical AND, statements are executed until FALSE is found. Of course, if the Logical OR never finds that TRUE value, then the expression evaluates to FALSE, and vice versa if Logical AND never finds that FALSE value.
Advertisement
I don''t really see the problem with using structures like this in code.. They aren''t confusing once you''ve seen them a few times. The problem is when you use this kind of thing because you think it will make your code faster.
quote:Original post by Qoy
I don''t really see the problem with using structures like this in code..

... written in Perl.
ok, why exactly would this make the code faster? Don''t they both get changed into conditional jump instructions?
quote:Original post by SabreMan
... written in Perl.

hehehe hahaha

http://roninmagus.hopto.org
acronymfinder.com - Find any acronym you need!

[edited by - Ronin Magus on August 28, 2002 12:06:12 PM]
it wont make anything faster and has the potential to make it slower and harder to read. newbs seem to have the idea in their heads that crytic = speed, l33t, efficient. even though its not the case
Lazy evaluation is good for checking for null pointers before dereferencing them without having nested if''s...

if( ptr && ptr->doSomething )

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
I think it makes it easier to understand, because there''s less information to digest. If it''s a null check, it makes sesn to put it all in one conditional; they are one logical operation, they belong together.

And it''s a common technique, and it''s *important* that you know that C++ defaults to short-curcuit evaluation - so anyway you dice it, you need to know it.


quote:Qoy
The problem is when you use this kind of thing because you think it will make your code faster.

Why would you think that?
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement