Jeremy McNees
Jeremy McNees

Reputation: 787

Julia Control Flow Booleans not Working as Expected

I'm getting results that don't match my expectations when using short-circuit evaluation control flow. This is what I want to do:

if var1 || var2 && var3
 do something cool
end

instead I get this:

if var1 || var2 
 do something that I don't want
end

this seems to only evaluate var1 || var2 and skips the && part. I think this should work so, what am I missing?

link to control-flow: http://julia.readthedocs.org/en/latest/manual/control-flow/

Thanks

The solution, as provided below, shows that I should've used parenthesis to get what I want:

  if (var1 || var2) && var3
     do something cool
    end

Upvotes: 3

Views: 97

Answers (1)

StefanKarpinski
StefanKarpinski

Reputation: 33259

The && operator has higher precedence than || which means your test is equivalent to var1 || (var2 && var3) rather than (var1 || var2) && var3 which may be what you expect. This precedence is standard in many languages, including C, Java, Perl, and Ruby.

Upvotes: 6

Related Questions