Understanding Julia

Hello,
I am new to julia. I am trying to understand the logic behind the following code. I know that && is a logical and but what excatly does it mean in this case? I appreciate your help.

for i = 1:len(d)
    set = sets
    lb = foo(x,y)
    lb > best_cost && continue

    for v in set
        #do something
        end
    end
end

This is called short-circuit evaluation for control flow (explained here in the manual).

In short when you use && (reads “and”) the thing in the right side of the operator is evaluated only if the thing in the left evaluates to true (if the thing in the right is not true then it does not make sense to compute it).

So people use it to control the flow of a program. That part is equivalent to:

if lb > best_cost
   continue
end
for v in set
    #do something 
end

end

If lb > best_cost the thing loop continues to the next iteration without computing the inner for loop.

3 Likes

Thank you for your help!

1 Like

How would you read || in case it was in place of &&?

Find out by trying it in the REPL!

julia> true || "hello"
?

julia> false || "hello"
?

When used for control flow in this way, you can read && as “and if so, then” and || as “or else”.

2 Likes