# How to avoid for blocks to evaluate

**URL:** https://discourse.julialang.org/t/how-to-avoid-for-blocks-to-evaluate/24430
**Category:** New to Julia
**Tags:** question
**Created:** [May 21, 2019, 1:27pm UTC](https://discourse.julialang.org/t/how-to-avoid-for-blocks-to-evaluate/24430 "2019-05-21T13:27:39Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Jaime](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jaime/32/8542_2.png) [@Jaime](https://discourse.julialang.org/u/Jaime)
#### Post date: [May 21, 2019, 1:27pm UTC](https://discourse.julialang.org/t/how-to-avoid-for-blocks-to-evaluate/24430/1 "2019-05-21T13:27:39Z")

</div>

I learned some blocks like if or begin or short-circuit evaluate last expression and they also have values(I don’t know concise words.)

```julia
1 + if true
x=10
y=20
end

```

this results 21.

```julia
false || 1+begin
x=10
y=20
end

```

this results 21.

Does this behavior cost additional memories? if it is, how can I avoid the behavior when I prefer that?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [May 21, 2019, 1:44pm UTC](https://discourse.julialang.org/t/how-to-avoid-for-blocks-to-evaluate/24430/2 "2019-05-21T13:44:36Z")

</div>

> [@Jaime](#):
>
> I learned some blocks like if or begin or short-circuit evaluate last expression

It is unclear what you mean here, but it is probably not correct. Where did you encounter this?

`if` and `begin` don’t short-circuit anything, but it is correct that their value is the value of the last contained expression.

The _compiler_ may optimize out certain expressions if it can prove that this does not change anything. Eg in

```julia
julia> function donothing(x)
       y = x^2 + 9*x - 1
       x
       end
donothing (generic function with 1 method)

julia> 

julia> @code_llvm donothing(1)

; @ REPL[4]:2 within `donothing'
define i64 @julia_donothing_16331(i64) {
top:
; @ REPL[4]:3 within `donothing'
  ret i64 %0
}

```

---

<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: [May 21, 2019, 2:06pm UTC](https://discourse.julialang.org/t/how-to-avoid-for-blocks-to-evaluate/24430/3 "2019-05-21T14:06:47Z")

</div>

They cost nothing when the result is not used, at least in local scope.  
They of course cost something when you run them in the repl, I.e. the printing in the repl is not free, but that can be suppressed with ; as usual.

---

<div class="post-metadata">

### Author: ![Jaime](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jaime/32/8542_2.png) [@Jaime](https://discourse.julialang.org/u/Jaime)
#### Post date: [May 21, 2019, 3:19pm UTC](https://discourse.julialang.org/t/how-to-avoid-for-blocks-to-evaluate/24430/4 "2019-05-21T15:19:48Z")

</div>

Thanks for advice but I can’t read @code\_llvm informations yet. Could you give me a help?

---

<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: [May 21, 2019, 3:30pm UTC](https://discourse.julialang.org/t/how-to-avoid-for-blocks-to-evaluate/24430/5 "2019-05-21T15:30:53Z")

</div>

Please don’t suggest that when it’s unnecessary, if anything the typed and optimized Julia ir should be enough to show the optimization.

Note that the dead code elimination isn’t even relevant here. In both cases the op are comparing, the results are unused. (Either the result of the last expression being unused by the enclosing block or the result of the block, I.e. the result of the last expression being unused by the “caller”).

If you want to understand llvm IR (which is really unnecessary for almost all users) you can check [LLVM Language Reference Manual — LLVM 16.0.0git documentation](http://llvm.org/docs/LangRef.html).
