# Conditional test \`? :\` behave differently if switch the turn of return value

**URL:** https://discourse.julialang.org/t/conditional-test-behave-differently-if-switch-the-turn-of-return-value/51578
**Category:** General Usage
**Tags:** question, function
**Created:** [December 10, 2020, 2:27am UTC](https://discourse.julialang.org/t/conditional-test-behave-differently-if-switch-the-turn-of-return-value/51578 "2020-12-10T02:27:32Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![KyleJT](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kylejt/32/8929_2.png) [@KyleJT](https://discourse.julialang.org/u/KyleJT)
#### Post date: [December 10, 2020, 2:27am UTC](https://discourse.julialang.org/t/conditional-test-behave-differently-if-switch-the-turn-of-return-value/51578/1 "2020-12-10T02:27:32Z")

</div>

Currently, I worked on this homework, _i.e._ [Triangle](https://exercism.io/my/solutions/87674efbb283421e85110c8615aebd40). Some code snippet for test the isosceles is as following:

```julia
reset_pos(idx) = idx == 0 ? 1 : idx

function is_triangle(sides)
    i = 1
    while i <= length(sides)
        sides[i] + sides[reset_pos(mod(i+1,3))] > sides[reset_pos(mod(i+2,3))] ? i = i+1 : return false
    end
    return true
end

function is_isosceles(sides)
    if !is_triangle(sides) return false end
    i = 1
    while i <= length(sides)
        (sides[i] == sides[reset_pos(mod(i+1,4))]) ? return true : i=i+1 # strange thing happens here.
    end
    return false
end

# test 
is_isosceles([1,2,2])

```

if the above code annotated is

```julia
(sides[i] == sides[reset_pos(mod(i+1,4))]) ? return true : i=i+1

```

The compiler warns `ERROR: LoadError: syntax: colon expected in "?" expression`. **However** , if the above code annotated is changed into

```julia
!(sides[i] == sides[reset_pos(mod(i+1,4))]) ? i=i+1 : return true

```

` is_isosceles(sides)` works normally.

I wonder if I missed something. What caused the annotated code snippet behaves wrong?

Thank you.

---

<div class="post-metadata">

### Author: ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)
#### Post date: [December 10, 2020, 2:41am UTC](https://discourse.julialang.org/t/conditional-test-behave-differently-if-switch-the-turn-of-return-value/51578/2 "2020-12-10T02:41:49Z")

</div>

Just a guess, but try wrapping the `return` statement in parentheses - that is `... ? (return true) : ...`. I’ve found that most syntax errors I make can be solved with judicious use of parentheses.

I don’t understand it in detail, but it’s something to due with how the parser breaks down your code into expressions. Things like `return` and `=` for assignment are special - I’m guessing that `stuff ? retun foo : bar` gets parsed as `(stuff) ? (return foo : bar)` rather than what you intended `(stuff) ? (return foo) : (bar)`

---

<div class="post-metadata">

### Author: ![KyleJT](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kylejt/32/8929_2.png) [@KyleJT](https://discourse.julialang.org/u/KyleJT)
#### Post date: [December 10, 2020, 2:48am UTC](https://discourse.julialang.org/t/conditional-test-behave-differently-if-switch-the-turn-of-return-value/51578/3 "2020-12-10T02:48:31Z")

</div>

Your solution works. Some revisions like the following works. Anyway, let’s wait to see if anyone can offer more details. Thank you.

```julia
(sides[i] == sides[reset_pos(mod(i+1,4))]) ? (return true) : i=i+1

```

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [December 10, 2020, 3:06am UTC](https://discourse.julialang.org/t/conditional-test-behave-differently-if-switch-the-turn-of-return-value/51578/4 "2020-12-10T03:06:00Z")

</div>

> [@KyleJT](#):
>
> ```julia
> return true : i=i+1
> 
> ```

This is parsed as `return (true : i=i+1)`

Just don’t return in `?:`, use a proper `if`.

---

<div class="post-metadata">

### Author: ![KyleJT](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kylejt/32/8929_2.png) [@KyleJT](https://discourse.julialang.org/u/KyleJT)
#### Post date: [December 10, 2020, 12:43pm UTC](https://discourse.julialang.org/t/conditional-test-behave-differently-if-switch-the-turn-of-return-value/51578/5 "2020-12-10T12:43:58Z")

</div>

thank you very much. 囧rz
