FAQ Database Discussion Community


Does all conditions get checked in an if statement even if the first one is false?

logic,short-circuiting
If I have: bool foo = false; bool bar = true; If(foo && bar) Doea the if-statement check if bar is true or does it skip it because foo already is false?...

How to invoke short-circuit logical operators in MATLAB as a function?

matlab,operator-overloading,logical-operators,low-level,short-circuiting
MATLAB operators usually translate into a function form as in the following examples: ~A => not(A) A + B => plus(A,B) A(...) => subsref(...) A(...) = ... => subsasgn(...) etc. Now please consider the operators && and ||. The various documentation (1-doc for or, 2-doc for and, 3-the MATLAB Programming...

Objective-C && doesn't short circuit for !var

objective-c,short-circuiting,evaluate
Try running this: UIView *testView = nil; NSLog(@"Take 1"); NSString *message = @"view doesn't exist"; if (!testView && !testView.subviews) { message = @"this message should never appear" ; } NSLog(message); NSLog(@"Take 2"); message = @"view doesn't exist"; if (testView != nil && testView.subviews != nil) { message = @"this message...

Java logical operator (&&, ||) short-circuit mechanism

java,logical-operators,short-circuiting
As I was reading a colleague's Java code, I stumbled upon an army of if/else statements. In these statements, several && and || operators were fighting each other without any help from parenthesis. I simplified the statements into: if (true || true && false) return true; else return false; What...

Logical short-circuit inside a function handle

matlab,lazy-evaluation,anonymous-function,logical-operators,short-circuiting
I have a function handle that operates on 2d arrays of arbitrary size: R2T = @(DL1,DL2) arrayfun(@(DL1,DL2)... 1/(fzero(@(x)fFitObj1(x)./fFitObj2(x)-... DL1./DL2,[minLim maxLim])) ... ,DL1,DL2) - C1; Here's a bottom-up breakdown of what it does: fzero(@(x)fFitObj1(x)./fFitObj2(x)-DL1./DL2,[minLim maxLim]) - This bit looks for a zero of the considered function on the interval [minLim maxLim],...

logical short-circuit and lambdas

c#,lambda,short-circuiting
I have the following lambda expression: response = allDescendants .Where(n => n.Caption.Contains(query) || n.Identifier.ToString().Contains(query) || n.Type.ToString().Contains(query) || n.Path.Contains(query) || n.Description.Contains(query) || (n.KeyWords != null && n.KeyWords.Any(kw => kw.Contains(query))) || n.SubType.Contains(query) || n.GroupingBy.Contains(query) ).ToList(); in the class definition the keyWord field is set to null: private string[] keyWords = null; public...

sh boolean short circuit variable assignment

bash,sh,variable-assignment,short-circuiting
I'm trying to write a script that takes flags as parameters, so I want to be able to handle them being passed in any order. This is what I have so far: #!/bin/sh numargs=$# echo $numargs check_param () { if [ "$1" = "$2" ]; then return 1 else return...

One-line short-circuit evaluation with && || in JavaScript

javascript,ternary-operator,short-circuiting
var prefix = options && options.prefix || ''; In JavaScipt in my case. Can someone explain what kind of statement or condition is this? Or at the end what's the value of prefix variable? I know about(ternary operator): condition ? expr1 : expr2 but this was different....

JavaScript short circuit evaluation error?

javascript,conditional-statements,conditional-operator,typeof,short-circuiting
The two below snippets of JS code have had me confused, in my eyes both should work the same, due to short circuit evaluation. But for some reason snippet '1' causes the error (on the third line): Cannot read property 'match' of undefined Array 'a' holds 3 character values user...