Talk:Short-circuit evaluation
From Wikipedia, the free encyclopedia
[edit] Article title
Can anyone motivate (with a reference) the term "minimal evaluation"? I've only seen this concept referred to as short-circuit evaluation (in compiler literature). Wouter Lievens 14:33, 19 May 2006 (UTC)
[edit] Problems
Added a brief segue into problems that short-circuit evaluation may cause (not executing the second part of a conditional if that conditional is supposed to do something.), since every programming book I've ever seen mentions this at least briefly. Sloverlord 15:00, 25 October 2006 (UTC)
It is not true that a evaluates to false. a evaluates to 0. The logical operators in C recognize zero and nonzero, not true and false. If you wish to use Boolean terminology, it would be better to give code examples in a language that actually has a bool type. Penry 22:38, 9 January 2007 (UTC)
[edit] Inaccuracy?
Quote:
In others (C, Ada), both short-circuit and standard boolean operators are available.
What standard boolean operator is available in C? I didn't know C has boolean operators at all, just logical ones (difference being that a logical operator evaluates something to be logical true or false and a boolean operator evaluates to a typed true or false statement). I suppose you could write the first example as (assuming a and myfunc are both int and long has more bits):
if ((long)a + (long)myfunc(b)) { do_something(); }
This would cause a and myfunc to be evaluated and then convert to a larger type so the results can't overflow and get back to 0 so the addition acts like an &&
, but I hardly think that anyone would consider + a boolean operator. --216.37.197.174 14:42, 8 March 2007 (UTC)
Well, of course C has loose typing, and doesn't have a separate boolean type (neither does Lisp), but it uses integer 0 as False and integer 1 as True (in all integer types), though it also accepts all non-0 integers as True in conditionals. These are the values returned by the relational operators as well as by && and ||. On the other hand, though & and | function as boolean AND and OR for the standard True and False values, they also function as bit-wise AND and OR for other values. With that convention, "&" is the boolean AND and "|" is the boolean OR. "&&" is the short-circuit AND and "||" the short-circuit OR. Interestingly, 3 && 5 => 1 (not 5, as it would in Lisp), 3 || 5 => 1 (not 3, as it would in Lisp), and 0 || 5 => 1 (not 5, as it would in Lisp). So I think it is perfectly sensible to say that C and Lisp have boolean short-circuit operators, even though they aren't as clear-cut as in more strictly typed languages. --Macrakis 16:19, 8 March 2007 (UTC)