Reputation: 75
I am trying to run a while
cycle with or
and and
conditions in Julia.
In Python I could do
while something and somethingelse:
staff happens
And the same thing with the or
. But in Julia, even using the |
and &
operators, it doesn't seems to work, i.e.
t=0
while s != 0 & d <= N
t+=1
something happens
end
For the &
condition, I made it work by using an if
with the brake
condition i.e.
t=0
while s != 0
something happens
t+=1
if d>N
break
end
end
But I have no idea how to make it work for the or
.
If it can be done in a single line, even better.
Upvotes: 2
Views: 1383
Reputation: 176750
In Julia &
actually performs bitwise-and and |
performs bitwise-or. These operators have higher precedence than comparison operators so your condition is evaluated here as
s != (0 & d) <= N
which is probably not what was intended.
You need to use &&
and ||
for the equivalent of Python's and
and or
operators. These operators perform short-circuit evaluation (documentation).
As in Python, comparisons (like !=
and <=
) have higher precedence than boolean operators, so you shouldn't need additional parentheses.
Upvotes: 4