Adrian Torrie
Adrian Torrie

Reputation: 2844

Evaluating multiple OR conditions

Due to short circuit rules I can't write something like the this:

message = "nada"

if message != "success" || message != "login successful"
    error("get_response() failed with message returned of: $message")
end

Instead I've had to write the something like this to get it working:

message = "nada"

if message != "success" ? message != "login successful" : false
    error("get_response() failed with message returned of: $message")
end

The second way seems ... "clunky", so I feel like I've missed something when going through the julia manual. Is there a standard way to write multiple OR conditions? (or a better way to write multiple OR conditions than what I have).

Assume the message could be any string, and I only want to do something if it isn't "success" or "login successful".

Upvotes: 0

Views: 708

Answers (1)

Toivo Henningsson
Toivo Henningsson

Reputation: 2699

This simply seems to be a mistake in the use of or/and vs negations. I guess that what you wanted was

message = "nada"

if !(message == "success" || message == "login successful")
    error("get_response() failed with message returned of: $message")
end

or equivalently

message = "nada"

if message != "success" && message != "login successful"
    error("get_response() failed with message returned of: $message")
end

which both work for me. The original condition

message != "success" || message != "login successful"

is always true since any message has to be unequal to at least one of the strings "success" and "login successful".

Upvotes: 4

Related Questions