# If condition with a vector comparison

**URL:** <https://discourse.julialang.org/t/if-condition-with-a-vector-comparison/116256>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [June 26, 2024, 12:23pm UTC](https://discourse.julialang.org/t/if-condition-with-a-vector-comparison/116256 "2024-06-26T12:23:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [June 26, 2024, 12:23pm UTC](https://discourse.julialang.org/t/if-condition-with-a-vector-comparison/116256/1 "2024-06-26T12:23:27Z")

</div>

Hello, im trying to make a if condition where I can compare a variable to any value of vector and if it matches to any condition, it counted as true…

Example:

```julia
a = 3

if a == any([1, 2, 3, 4]) # imaginary function any() which will be more like a iterator..

   print("true")

else
 
  print("false")

end

```

Is there a function like any() in this example?

Thank you

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [June 26, 2024, 12:29pm UTC](https://discourse.julialang.org/t/if-condition-with-a-vector-comparison/116256/2 "2024-06-26T12:29:34Z")

</div>

Yes! It is called `any` 🙂

```julia
any(a .== [1, 2, 3, 4])

```

The line above uses broadcasting to compare `a` to each of the listed numbers. As a result, the argument of `any` is a vector of `Bool` values, and `any` returns `true` if the argument collection contains any `true`.

This is probably the most straightforward way, but not the most efficient.

Another way is to use

```julia
any(==(a), [1,2,3,4])
# or equivalently
any([1,2,3,4]) do x
    x == a
end

```

which does the same thing but avoids creating the intermediate vector of `Bool`s.

EDIT: Haha, @lmiq’s suggestion is definitely better if you are interested in equality comparison. Use `any` when the condition is not equality.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [June 26, 2024, 12:29pm UTC](https://discourse.julialang.org/t/if-condition-with-a-vector-comparison/116256/3 "2024-06-26T12:29:57Z")

</div>

```julia
a in [1,2,3,4]

a in 1:4

```

?

---

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [June 26, 2024, 12:35pm UTC](https://discourse.julialang.org/t/if-condition-with-a-vector-comparison/116256/4 "2024-06-26T12:35:49Z")

</div>

Am I doing something wrong here ? its not printing - yes.

```julia
a = 3
if a in [1, 2, 3, 4] == true
           print("yes")  
else 
          print("no")
end

```

---

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [June 26, 2024, 12:37pm UTC](https://discourse.julialang.org/t/if-condition-with-a-vector-comparison/116256/5 "2024-06-26T12:37:54Z")

</div>

this works

```julia
if any(a .== [1, 2, 3, 4]) == true
           print("yes") 
 else  
          print("false")
end

```

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [June 26, 2024, 12:38pm UTC](https://discourse.julialang.org/t/if-condition-with-a-vector-comparison/116256/6 "2024-06-26T12:38:02Z")

</div>

It’s about operator precedence. The condition is evaluated as

```julia
a in ([1, 2, 3, 4] == true)

```

not as

```julia
(a in [1, 2, 3, 4]) == true

```

You can simply use

```julia
a = 3
if a in [1, 2, 3, 4]
    print("yes")  
else 
    print("no")
end

```

---

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [June 26, 2024, 12:42pm UTC](https://discourse.julialang.org/t/if-condition-with-a-vector-comparison/116256/7 "2024-06-26T12:42:02Z")

</div>

thank you @barucden and @lmiq for help :).

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [June 26, 2024, 12:53pm UTC](https://discourse.julialang.org/t/if-condition-with-a-vector-comparison/116256/8 "2024-06-26T12:53:30Z")

</div>

> [@Sahil\_Khan](#):
>
> `if a in [1, 2, 3, 4] == true`

It’s not just about operator precedence, though. If you have a boolean condition, for example `mycond`, you should not write

```julia
if mycond == true
    # do something
end

```

The `== true` part is redundant, and not considered idiomatic. You should always just write

```julia
if mycond
    # do something
end

```

or

```julia
if !mycond # this means that mycond is false
    # do something else
end

```

Any time you see `== true` or `== false`, you should consider whether something is off.
