Conditional statements in Julia

Viewed 86

I define two non-empty vectors:

tmp = [1, 2, 3]
tmp2 = [1, 2]

When I type

size(tmp, 1) > 0 

The output is true. But when I write

size(tmp, 1) > 0 & size(tmp2, 1) > 0

it returns false. When I put each term into a parentheses, as in

(size(tmp, 1) > 0) & (size(tmp2, 1) > 0 )

it returns true as expected. But I don't understand why size(tmp, 1) > 0 & size(tmp2, 1) > 0 returns false?

1 Answers

This is a result of 2 things. The first is operator precedence. The code you wrote is parsed as size(tmp, 1) > (0 & size(tmp2, 1)) > 0 (ie as a chained comparison).

The bigger issue, however is that you probably wanted to use && instead of &. & is a bitwise and, while && is a logical and. As such, && has the precedence you expect here.

Related