# If then statement vs Grid

**URL:** https://discourse.julialang.org/t/if-then-statement-vs-grid/83537
**Category:** General Usage
**Created:** [June 29, 2022, 11:55pm UTC](https://discourse.julialang.org/t/if-then-statement-vs-grid/83537 "2022-06-29T23:55:11Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)
#### Post date: [June 29, 2022, 11:55pm UTC](https://discourse.julialang.org/t/if-then-statement-vs-grid/83537/1 "2022-06-29T23:55:12Z")

</div>

My goal is to create a piecewise function in Julia parsimoniously.  
u(x,y) = y if x = 0  
u(x,y) = y -1 if x \neq 0  
I get the following:

```julia
julia> FC(x) = (x==0):(0):(1)
FC (generic function with 1 method)

julia> u(x,y) = y - FC(x)
u (generic function with 1 method)

julia> u(1,0)
ERROR: ArgumentError: step cannot be zero
Stacktrace:
 [1] steprange_last(start::Int64, step::Int64, stop::Int64)
   @ Base .\range.jl:221
 [2] StepRange
   @ .\range.jl:208 [inlined]
 [3] StepRange
   @ .\range.jl:263 [inlined]
 [4] _colon
   @ .\range.jl:24 [inlined]
 [5] Colon
   @ .\range.jl:22 [inlined]
 [6] Colon
   @ .\range.jl:10 [inlined]
 [7] FC
   @ .\REPL[3]:1 [inlined]
 [8] u(x::Int64, y::Int64)
   @ Main .\REPL[4]:1
 [9] top-level scope
   @ REPL[5]:1

```

Problem is I want the function `FC(x) = (x==0):(0):(1)` to return 0 for x=0 and 1 for x\neq 0.  
Instead of being an if/else statement, this function returns a `StepRange`.

And yes I know how to do this the long way…

---

<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: [June 29, 2022, 11:57pm UTC](https://discourse.julialang.org/t/if-then-statement-vs-grid/83537/2 "2022-06-29T23:57:43Z")

</div>

> [@Albert\_Zevelev](#):
>
> My goal is to create a piecewise function in Julia parsimoniously.

I’m not sure why you’re using ranges for this. Why not:

```julia
u(x,y) = y - (x != 0)

```

or

```julia
u(x,y) = x == 0 ? y : y - 1

```

?

> [@Albert\_Zevelev](#):
>
> Problem is I want the function `FC(x) = (x==0):(0):(1)` to return 0 for x=0 and 1 for x\neq 0.

That’s just the function `FC(x) = x != 0`, or simply the function `!iszero`. (A boolean value `true`/`false` in Julia corresponds to `1`/`0`, respectively.)

---

<div class="post-metadata">

### Author: ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)
#### Post date: [June 30, 2022, 12:01am UTC](https://discourse.julialang.org/t/if-then-statement-vs-grid/83537/3 "2022-06-30T00:01:00Z")

</div>

I totally forgot the `?` in the if/then.  
Here is what I wanted:  
`u(x,y) = y - ( (x == 0) ? 0 : 1)`

---

<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: [June 30, 2022, 12:01am UTC](https://discourse.julialang.org/t/if-then-statement-vs-grid/83537/4 "2022-06-30T00:01:52Z")

</div>

> [@Albert\_Zevelev](#):
>
> Here is what I wanted:  
> `u(x,y) = y - ( (x == 0) ? 0 : 1)`

i.e. `y - (x != 0)`.
