# One-line if-else statement with short-circuit evaluation

**URL:** <https://discourse.julialang.org/t/one-line-if-else-statement-with-short-circuit-evaluation/85399>\
**Category:** General Usage\
**Created:** [August 6, 2022, 2:25pm UTC](https://discourse.julialang.org/t/one-line-if-else-statement-with-short-circuit-evaluation/85399 "2022-08-06T14:25:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tapyu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tapyu/32/37182_2.png) [@tapyu](https://discourse.julialang.org/u/tapyu)\
**Post date:** [August 6, 2022, 2:25pm UTC](https://discourse.julialang.org/t/one-line-if-else-statement-with-short-circuit-evaluation/85399/1 "2022-08-06T14:25:18Z")

</div>

In Unix shell scripts, we can do the following

```julia
❯ [[-n $HOME]] && echo "true statement" || echo "else statement"
true statement
❯ [[-n $NOT_A_ENV]] && echo "true statement" || echo "else statement"
else statement

```

However, it does not work in Julia

```julia
julia> true && println("true statement") || println("else statement")
true statement
ERROR: TypeError: non-boolean (Nothing) used in boolean context
Stacktrace:
 [1] top-level scope
   @ REPL[15]:1

```

Is there a similar feature in Julia (in one line, of course)?

PS: I put short-circuit evaluation in the title as I suppose it were possible, but solution with other approaches is welcome as well 🙂

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [August 6, 2022, 2:28pm UTC](https://discourse.julialang.org/t/one-line-if-else-statement-with-short-circuit-evaluation/85399/2 "2022-08-06T14:28:34Z")

</div>

The ternary operator is a one-line abbreviation for an `if-else` block.

```julia
julia> true ? println("true statement") : println("else statement")
true statement

julia> false ? println("true statement") : println("else statement")
else statement

```
