Talk:Short-circuit evaluation

From Wikipedia, the free encyclopedia

Contents

[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)

...and evaluate to 1 and 0. (which are sometimes considered as true and false) ca1 (talk) 00:36, 12 May 2008 (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)

You're not kidding. The implication that an Ada 'and' is equivalent to a C '&' is brutally misleading. 209.139.199.1 (talk) 17:49, 29 January 2008 (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)

An explicit native bool type was added in C99, it is called _Bool, and a header <stdbool.h> that defines bool as a macro that expands to _Bool --.oisyn 10:14, 31 August 2007 (UTC)

[edit] better example

 if(0!=b && a/b) {
     printf("divided by %d",b);
 }
  • i always thought this is the one of best examples for such thing. for such example shows something you should avoid anyway.
    • strlen(0) is valid last time i checked ;) - even though it depends on library :)
  • another one, can be seen in linux kernel. in same spirit is this one.
 int finished_with_condition_2=1; // set to zero if loop finished with condition 2
 do {
    if(some_condition_1())
          break;
 } while(some_condition_2() || (finished_with_condition_2=0));
  • unix shell uses && and || for evaluate if first was ok, evaluate if first was not ok, respectively.

ca1 (talk) 00:34, 12 May 2008 (UTC)