# Why does "Doesn't exist" always print?

**URL:** https://discourse.julialang.org/t/why-does-doesnt-exist-always-print/128427
**Category:** New to Julia
**Created:** [April 26, 2025, 8:16am UTC](https://discourse.julialang.org/t/why-does-doesnt-exist-always-print/128427 "2025-04-26T08:16:05Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)
#### Post date: [April 26, 2025, 8:16am UTC](https://discourse.julialang.org/t/why-does-doesnt-exist-always-print/128427/1 "2025-04-26T08:16:06Z")

</div>

Hello,  
Please take a look at the following code:

```julia
function search()
    array = [1,2,3,4,5]
    low = 0
    high = 0
    while true
        print("Please enter two numbers for Low and high index: ")
        low, high = parse.(Int, split(readline()))
        if (low <=0) || (high > length(array))
            println("Wrong range.")
        else
            break
        end
    end
    print("Please enter a number to check if it exists: ")
    n = parse.(Int, readline())
    flag = 0
    for i = low : high 
        if n == array[i]
           println("Exist.")
           break
        else
            flag += 1
        end
    end
    if flag > 0
        println("Doesn't exist.")
    end
end
search()

```

Why does `Doesn't exist` always print?

Thank you.

---

<div class="post-metadata">

### Author: ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)
#### Post date: [April 26, 2025, 8:34am UTC](https://discourse.julialang.org/t/why-does-doesnt-exist-always-print/128427/2 "2025-04-26T08:34:42Z")

</div>

If you enter `low = 1, high = 5` and `n = 1` then it doesn’t show the `Doesn't exist.` message.

The issue is that `flag` is increased whenever the first entry is not equal to `n`. Instead you might want to change the logic for example like

```julia
found = false
for i = low : high 
    if n == array[i]
        println("Exist.")
        found = true
        break
   end
end
if !found
    println("Doesn't exist.")
end

```

---

<div class="post-metadata">

### Author: ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)
#### Post date: [April 26, 2025, 8:47am UTC](https://discourse.julialang.org/t/why-does-doesnt-exist-always-print/128427/3 "2025-04-26T08:47:08Z")

</div>

Or just use something like

```julia
println(isnothing(findfirst(==(n), @view array[low:high])) ? "Doesn't exist" : "Exist.")

```

which imho is easier to get correct … the one-liner is just for fun though, e.g., multi-line `if` might be clearer.

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [April 26, 2025, 9:40am UTC](https://discourse.julialang.org/t/why-does-doesnt-exist-always-print/128427/4 "2025-04-26T09:40:02Z")

</div>

Putting the fixed bug aside,

1. You don’t need to broadcast over scalars, `n = parse(Int, readline())` is enough.
2. Why are you hard-coding the `array` values and taking `low, high, n` from separated `readline()` calls instead of easier arguments for the `search()` call?

---

<div class="post-metadata">

### Author: ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)
#### Post date: [April 26, 2025, 10:44am UTC](https://discourse.julialang.org/t/why-does-doesnt-exist-always-print/128427/5 "2025-04-26T10:44:39Z")

</div>

Hello,  
Thank you so much for your reply.  
I found another way:

```julia
flag = 0
    for i = low : high 
        if n == array[i]
           println("Exist.")
           flag = 0
           break
        else
            flag +=1
        end
    end
    if flag > 0
        println("Doesn't exist.")
    end

```

---

<div class="post-metadata">

### Author: ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)
#### Post date: [April 26, 2025, 10:46am UTC](https://discourse.julialang.org/t/why-does-doesnt-exist-always-print/128427/6 "2025-04-26T10:46:45Z")

</div>

Hello,  
Do you mean the `search()` function with input arguments?

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [April 26, 2025, 8:06pm UTC](https://discourse.julialang.org/t/why-does-doesnt-exist-always-print/128427/7 "2025-04-26T20:06:57Z")

</div>

yes

---

<div class="post-metadata">

### Author: ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)
#### Post date: [April 27, 2025, 4:29am UTC](https://discourse.julialang.org/t/why-does-doesnt-exist-always-print/128427/8 "2025-04-27T04:29:35Z")

</div>

Mh, there is the potential edge case of an empty range, such as `low = 2, high = 1, n = 10`. Here your code doesn’t print anything, whereas you might either add a check `low < high` or print `Doesn't exists.`

---

<div class="post-metadata">

### Author: ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)
#### Post date: [April 27, 2025, 5:31am UTC](https://discourse.julialang.org/t/why-does-doesnt-exist-always-print/128427/9 "2025-04-27T05:31:32Z")

</div>

Hi,  
Great advice.
