# One-line \`if ... then ...\` syntax

**URL:** <https://discourse.julialang.org/t/one-line-if-then-syntax/79573>\
**Category:** Internals & Design\
**Tags:** syntax\
**Created:** [April 16, 2022, 2:27pm UTC](https://discourse.julialang.org/t/one-line-if-then-syntax/79573 "2022-04-16T14:27:12Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [April 19, 2022, 12:20am UTC](https://discourse.julialang.org/t/one-line-if-then-syntax/79573/21 "2022-04-19T00:20:07Z")

</div>

You can use parentheses for that

```julia
julia> true && (x=nothing) # nothing

```

though I’m not sure how that’s

```julia
  Short-circuiting boolean AND.

```

---

<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:** [April 19, 2022, 5:04am UTC](https://discourse.julialang.org/t/one-line-if-then-syntax/79573/22 "2022-04-19T05:04:13Z")

</div>

Is this a somewhat contrived example designed only to demonstrate issues with `&&`? Because it seems like a Python-ism.

In Julia, you would do

```julia
function bar(x::AbstractVector, m::Int=length(x))
   ... 
end

```

(Well, _actually_ you would avoid the slice, etc, by writing two entirely separate methods.)

---

<div class="post-metadata">

**Author:** ![maxkapur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxkapur/32/21208_2.png) [@maxkapur](https://discourse.julialang.org/u/maxkapur)\
**Post date:** [April 19, 2022, 5:34am UTC](https://discourse.julialang.org/t/one-line-if-then-syntax/79573/23 "2022-04-19T05:34:14Z")

</div>

Whoa, since when does this work? (Since forever, probably.) You’re right: This is a leftover Python habit.

---

<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:** [April 19, 2022, 6:12am UTC](https://discourse.julialang.org/t/one-line-if-then-syntax/79573/24 "2022-04-19T06:12:46Z")

</div>

Yes, basically forever😄 It’s implemented via multiple dispatch, so you will see that it defines two separate methods.

---

<div class="post-metadata">

**Author:** ![kodintent](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kodintent/32/32225_2.png) [@kodintent](https://discourse.julialang.org/u/kodintent)\
**Post date:** [April 22, 2022, 5:06am UTC](https://discourse.julialang.org/t/one-line-if-then-syntax/79573/25 "2022-04-22T05:06:13Z")

</div>

Hi.  
i really hope that any IF must have an END, without (start of line) exception.  
the required END really helps me with organizing and reading, so i would not like to have this pattern broken with an IF that cant have an END. I wouldnt use it in my code because it would definitely throw me off.  
when it comes to && vs then, || vs or, etc. im fine with both, but i prefer typing and reading then/and/or/…  
im fine with shortcut versions that dont have IF-END. why not?

---

<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:** [April 22, 2022, 6:59am UTC](https://discourse.julialang.org/t/one-line-if-then-syntax/79573/26 "2022-04-22T06:59:46Z")

</div>

Comprehensions can have `if` without `end`, like this:

```julia
[x for x in 1:10 if iseven(x)]

```

But I think this is still quite clear and easy to read.

---

<div class="post-metadata">

**Author:** ![kodintent](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kodintent/32/32225_2.png) [@kodintent](https://discourse.julialang.org/u/kodintent)\
**Post date:** [April 22, 2022, 7:17am UTC](https://discourse.julialang.org/t/one-line-if-then-syntax/79573/27 "2022-04-22T07:17:20Z")

</div>

hi. thanks for pointing out this different use of if.  
im still confident that everyone knows i mean “if” at the start of the line.

[Previous page](https://discourse.julialang.org/t/one-line-if-then-syntax/79573.md?page=1)
