Do all programming languages ​​have a logical short circuit rating?

In php code

if(a() && b()) 

when the first operand evaluates to false , b() will not evaluate.

Similarly, in

 if (a() || b()) 

when the first operand evaluates to true , b() will not be evaluated ..

This is true for all languages ​​like Java, C #, etc.

This is the test code we used.

 <?php function a(){ echo 'a'; return false; } function b(){ echo 'b'; return true; } if(a() && b()){ echo 'c'; } ?> 
+35
language-agnostic if-statement short-circuiting
Aug 05 '09 at 11:16
source share
17 answers

This is called a short circuit rating .

This rule holds true for languages ​​derived from C (C, C ++, Java, C #), but not for all languages.

For example, VB6 does not, and this was not done in earlier versions of VB.NET. VB8 (in Visual Studio 2005) introduced AndAlso and OrElse operators for this purpose.

In addition, from the comments, it seems that csh is evaluating the short circuit from right to left to further confuse the situation.

It should also be noted that short circuit assessment (or absence) has its own hazards to be aware of. For example, if the second operand is a function that has any side effects, then the code may not work as the programmer intended.

+62
Aug 05 '09 at 11:19
source share

This is not true for VB6.

In VB.net, you need to use "AndAlso" instead of "And" if you want it to skip evaluating the second expression.

+20
Aug 05 '09 at 11:18
source share

Is this true for ALL languages ​​such as JAVA, C #, etc.?

In C #, this is true only for the short circuit operators " || " and " && "; if you just use ' | 'or' & ', it will evaluate both sides each time.

+17
Aug 05 '09 at 11:24
source share

It is called short circuit assessment , and most languages ​​do it. Some languages ​​have operators that do not.

+12
Aug 05 '09 at 11:19
source share

The original version of Pascal did not do this, which caused a lot of grief. Modern Pascals, such as Delphi, work just like C and others.

+8
Aug 05 '09 at 11:21
source share

This is true for languages ​​that are the “children” of C: PHP, Java, C ++, C #, ... or in the same “inspiration” as Perl.

But this does not apply to VB (at least until .NET, which introduced new keywords for this).
(And that really bothers the first thing you work with VB ^^)

+5
Aug 05 '09 at 11:20
source share

Ada has special short-circuited forms of conditional expressions:

 and then or else 

used as follows:

 if p.next /= null and then p.next.name = 'foo' if x = 0 or else 1/x = y 

In a sense, this looks good, because you can conclude that the programmer knew that the expression should be shorted and that the conditional did not work by accident.

+5
Aug 05 '09 at 11:30 a.m.
source share

Microsoft VBScript (often used in conjunction with the Classic ASP) did not have a short circuit rating for logical operators; instead, it uses a bitwise rating. This is one of many reasons why this is arguably the worst language ever!

"What happens is that VBScript is illogical. VBScript is beaten. All the so-called logical operators work by numbers and not by logical values! Not, And, Or, XOr, Eqv and Imp all convert their arguments to four-byte integers, perform a logical operation on each pair of bits in integers and return the result. If True is -1 and False 0, then everything turns out because -1 has all its bits turned on and 0 all its bits are turned off. But if other numbers get there, all bids are off. "

Taken from this blog. Eric Lippert.

+4
Aug 05 '09 at 12:59
source share

This is true for Java, but the operators | etc. will be evaluated by both parties.

+3
Aug 05 '09 at 11:22
source share

In Delphi, this is a compiler option.

+3
Aug 05 '09 at 12:53
source share

This is called a short circuit rating, and it is common to all the languages ​​I have ever worked in (C, C ++, C #, Java, Smalltalk, Javascript, Lisp), with the exception of VB, VB.NET and Fortran.

This is actually a pretty useful feature. Without a short circuit, you cannot do this:

if (a! = null & a.isBlank ())

Without a short circuit, you will have to have nested if statements, because the second part will throw an error if a is null.

+2
Aug 05 '09 at 11:30
source share

Most languages ​​(everything I've seen) use a short circuit rating for CONDITIONAL statements such as && and ||. They will cease to evaluate as soon as one of the conditions satisfies this requirement. (The first value is false on &. The first true value is on ||)

All BINARY statements, such as and and |, are processed. (Original)

All BITWISE statements, such as and and |, are processed. (Editing: 5/10/17)

+2
Aug 05 '09 at 12:01
source share

In Erlang, the and and or operators do not evaluate a short circuit; you must use the orelse and andalso operators if you want a short circuit.

+2
Aug 05 '09 at 13:59
source share

Coldfusion will conduct a short circle assessment. I'm sure all CF developers have written:

 <cfif isdefined("somevariable") and somevariable eq something> //do logic </cfif> 
+1
Aug 05 '09 at 11:33
source share

MATLAB is one language that distinguishes between "standard" logical operators and short-circuit operators :

  • & (The operator AND) and | (OR operator) can work on arrays in an elementary way.
  • && and || are short circuit versions for which the second operand is evaluated only when the result is not completely determined by the first operand. They can only work on scalars, not arrays.
+1
Aug 05 '09 at 14:22
source share

In standard FORTRAN or Fortran, logical expression operands can be evaluated in any order. An incomplete assessment is permitted, but a specific implementation.

This allows you to optimize logical expressions that would not be resolved if strict ordering were observed from left to right. Expressions requiring strict ordering must be decomposed into separate conventions or assumptions may be made depending on implementation.

Since decomposition is used to provide ordering, it follows that individual IF statements cannot always be optimized into a single expression. However, short circuit assessment is explicit with decomposition, and it is never worse than languages ​​that provide strict ordering from left to right to give a lazy assessment.

Languages ​​that are derived from FORTRAN (Fortran, BASIC, VBn) and languages ​​that were designed to achieve performance similar to FORTRAN (Pascal, Ada) initially corresponded to the FORTRAN example, which allows out-of-order evaluation.

+1
Mar 17
source share

Other answers gave good examples of languages ​​with and without short circuit rating, so I will not repeat them.

Just one interesting point to add: Lisps, such as Clojure, have a logical short circuit rating, but you can also trivially define any statement you like with a short circuit rating using macros.

An example of the short-circuited "nand" operation in Clojure:

 (defmacro nand ([x] `(not ~x)) ([x & xs] `(let [nand# (not ~x)] (if nand# true ; short circuit if we can prove the nand is true (nand ~@xs))))) ; continue with the other expressions otherwise (nand true true) => false (nand false (println "Expression with a side effect!")) => true 
0
Nov 19 '11 at 8:55
source share



All Articles