# Multi-line expressions

**URL:** https://discourse.julialang.org/t/multi-line-expressions/16096
**Category:** General Usage
**Created:** [October 10, 2018, 3:13am UTC](https://discourse.julialang.org/t/multi-line-expressions/16096 "2018-10-10T03:13:47Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![albi3ro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albi3ro/32/1795_2.png) [@albi3ro](https://discourse.julialang.org/u/albi3ro)
#### Post date: [October 10, 2018, 3:13am UTC](https://discourse.julialang.org/t/multi-line-expressions/16096/1 "2018-10-10T03:13:47Z")

</div>

I remember being able to extend expressions over multiple lines without a problem in earlier versions of julia.

How do I wrap an expression over multiple lines now when it gets too long?

Particularly, I want to break at the equals sign below

```julia
for j in 2:Nz #copying the first layer into the entire cube
    X[(Ncell*Ny*Nx*(j-1) .+(1:Ncell*Nx*Ny)),:] =X[1:Ncell*Nx*Ny,:] .+(j-1)*cM;
end

```

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [October 10, 2018, 3:29am UTC](https://discourse.julialang.org/t/multi-line-expressions/16096/2 "2018-10-10T03:29:36Z")

</div>

Line breaks after `=` (or inside a parens, or after an binary operator like `.+`) are still fine.

---

<div class="post-metadata">

### Author: ![albi3ro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albi3ro/32/1795_2.png) [@albi3ro](https://discourse.julialang.org/u/albi3ro)
#### Post date: [October 10, 2018, 4:00am UTC](https://discourse.julialang.org/t/multi-line-expressions/16096/3 "2018-10-10T04:00:41Z")

</div>

ahh, was doing the break before the =  
silly mistake

---

<div class="post-metadata">

### Author: ![aitzkora](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aitzkora/32/14610_2.png) [@aitzkora](https://discourse.julialang.org/u/aitzkora)
#### Post date: [April 2, 2025, 1:48pm UTC](https://discourse.julialang.org/t/multi-line-expressions/16096/4 "2025-04-02T13:48:53Z")

</div>

Hi, I realize today, that if you use line breaks after `=`, the binary operator involved must be on that line. For instance, the following Juia code

```julia
N=4;z=zeros(N);
for i in eachindex(z)
    z[i] = i^2-i
end 
z2=similar(z)
for i in eachindex(z2)
    z2[i] = i^2 -
          i   
end 
z3=similar(z)
for i in eachindex(z3)
    z3[i] = i^2
          - i
end 
[z z2 z3]

```

gives

```julia
    4×3 Matrix{Float64}:
    0.0 0.0 1.0
    2.0 2.0 4.0
    6.0 6.0 9.0
   12.0 12.0 16.0

```

So using `-` on the following lines after a line breaks does not give the right answer

---

<div class="post-metadata">

### Author: ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)
#### Post date: [April 2, 2025, 2:30pm UTC](https://discourse.julialang.org/t/multi-line-expressions/16096/5 "2025-04-02T14:30:38Z")

</div>

Yes, in your example, `z3[i] = i^2` and `-i` are parsed and evaluated as separate expressions. (`-i` is evaluated in the loop but not saved to a variable.) If you want to write expressions this way, parentheses are your friend.

```julia
for i in eachindex(z3)
    z3[i] = (i^2
          - i)
end

```

I’ll amend what `@stevengj` wrote slightly:

> [@stevengj](#):
>
> Line breaks _immediately_ after `=` (or inside a parens, or _immediately_ after a binary operator like `.+`) are still fine.

---

<div class="post-metadata">

### Author: ![aitzkora](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aitzkora/32/14610_2.png) [@aitzkora](https://discourse.julialang.org/u/aitzkora)
#### Post date: [April 2, 2025, 2:39pm UTC](https://discourse.julialang.org/t/multi-line-expressions/16096/6 "2025-04-02T14:39:51Z")

</div>

Thanks!
